lubridate Exercises in R: 50 Real Practice Problems
Fifty practice problems on lubridate: parse, extract components, do date arithmetic, work with intervals and time zones. Hidden solutions.
Section 1. Parsing dates (8 problems)
Exercise 1.1: ymd
Difficulty: Beginner. Parse "2024-01-15".
Show solution
Exercise 1.2: mdy
Difficulty: Beginner. Parse "01/15/2024".
Show solution
Exercise 1.3: dmy
Difficulty: Beginner. Parse "15-01-2024".
Show solution
Exercise 1.4: ymd_hms
Difficulty: Intermediate. Parse "2024-01-15 14:30:00".
Show solution
Exercise 1.5: parse_date_time multi-format
Difficulty: Intermediate. Parse a vector with mixed formats.
Show solution
Exercise 1.6: From milliseconds since epoch
Difficulty: Advanced. Convert 1705305600 to POSIXct.
Show solution
Exercise 1.7: Parse with timezone
Difficulty: Advanced. Parse and set NY timezone.
Show solution
Exercise 1.8: From a numeric like 20240115
Difficulty: Intermediate. Parse 20240115 as a date.
Show solution
Section 2. Extract components (8 problems)
Exercise 2.1: Year
Difficulty: Beginner.
Show solution
Exercise 2.2: Month with label
Difficulty: Intermediate. Full month name.
Show solution
Exercise 2.3: Day of month
Difficulty: Beginner.
Show solution
Exercise 2.4: Day of week
Difficulty: Intermediate. With Monday-first abbreviated label.
Show solution
Exercise 2.5: Day of year (julian)
Difficulty: Intermediate.
Show solution
Exercise 2.6: Quarter
Difficulty: Intermediate.
Show solution
Exercise 2.7: Week of year
Difficulty: Intermediate.
Show solution
Exercise 2.8: Hour, minute, second
Difficulty: Intermediate.
Show solution
Section 3. Arithmetic (8 problems)
Exercise 3.1: Add days
Difficulty: Beginner. Add 7 days.
Show solution
Exercise 3.2: Add weeks
Difficulty: Beginner. Add 4 weeks.
Show solution
Exercise 3.3: Add months
Difficulty: Intermediate. Add 3 months. Note period vs duration.
Show solution
Exercise 3.4: Subtract years
Difficulty: Intermediate. 5 years ago today.
Show solution
Exercise 3.5: Difference in days
Difficulty: Intermediate. Days between two dates.
Show solution
Exercise 3.6: Age in years
Difficulty: Intermediate. Age from birth date.
Show solution
Exercise 3.7: Add business days (approximation)
Difficulty: Advanced. Add 10 weekdays.
Show solution
Exercise 3.8: Roll to month end
Difficulty: Advanced.
Show solution
Section 4. Intervals and durations (8 problems)
Exercise 4.1: Interval object
Difficulty: Intermediate. Build an interval Jan 1 to Apr 30.
Show solution
Exercise 4.2: Duration in days
Difficulty: Intermediate. Same span as days.
Show solution
Exercise 4.3: Check overlap
Difficulty: Advanced. Do two intervals overlap?
Show solution
Exercise 4.4: Period vs duration
Difficulty: Advanced. Show difference using months().
Show solution
Exercise 4.5: Check if date in interval
Difficulty: Intermediate. Is "2024-03-15" inside Jan-Apr?
Show solution
Exercise 4.6: Length in months
Difficulty: Advanced.
Show solution
Exercise 4.7: Sum durations
Difficulty: Intermediate. Add 2 hours and 30 minutes.
Show solution
Exercise 4.8: Days remaining in month
Difficulty: Advanced.
Show solution
Section 5. Floor and round (8 problems)
Exercise 5.1: Floor to month
Difficulty: Intermediate.
Show solution
Exercise 5.2: Floor to week (Mon start)
Difficulty: Intermediate.
Show solution
Exercise 5.3: Ceiling to month
Difficulty: Intermediate.
Show solution
Exercise 5.4: Round to nearest hour
Difficulty: Intermediate.
Show solution
Exercise 5.5: Floor to quarter
Difficulty: Intermediate.
Show solution
Exercise 5.6: Group by month
Difficulty: Intermediate. Aggregate counts per month.
Show solution
Exercise 5.7: Floor to 15-minute bucket
Difficulty: Advanced.
Show solution
Exercise 5.8: Round to nearest day
Difficulty: Intermediate.
Show solution
Section 6. Time zones (5 problems)
Exercise 6.1: Set time zone
Difficulty: Intermediate. Force NY timezone.
Show solution
Exercise 6.2: Convert time zone
Difficulty: Advanced. NY -> Tokyo.
Show solution
Exercise 6.3: List available zones
Difficulty: Intermediate.
Show solution
Exercise 6.4: Daylight saving check
Difficulty: Advanced. Is "2024-07-01 NY" in DST?
Show solution
Exercise 6.5: Convert to UTC
Difficulty: Intermediate.
Show solution
Section 7. Real workflows (5 problems)
Exercise 7.1: Compute days between events
Difficulty: Intermediate. Per user, days from first to last event.
Show solution
Exercise 7.2: Build a daily calendar
Difficulty: Intermediate. All dates in Q1 2024.
Show solution
Exercise 7.3: Detect weekends
Difficulty: Beginner. Flag a date as weekend.
Show solution
Exercise 7.4: Group transactions by month-end
Difficulty: Advanced. Tag each row with its month-end.
Show solution
Exercise 7.5: Time-of-day buckets
Difficulty: Intermediate. Tag hour as morning/afternoon/evening.
Show solution
What to do next
- Date-Time-Manipulation-Exercises (coming), broader date workflows.
- tidyverse-Exercises (shipped), dates inside data pipelines.
- Time-Series-Exercises (coming), date-indexed analysis.