Strings and Dates
Dates
Adding Days
R Date values support calendar arithmetic. Adding a number to a Date moves by that many days.
Program
Play the script to compute a due date and format it for display.
dates.R
Replay: real traced execution (multi-file project)
start <- as.Date("2026-05-01")
due <- start + 14
label <- format(due, "%b %d")
cat(label, "\n", sep = "")
start ← 2026-05-01
1start <- as.Date("2026-05-01")2due <- start + 14values this step2026-05-01startdue ← 2026-05-15
1start <- as.Date("2026-05-01")2due <- start + 143label <- format(due, "%b %d")values this step2026-05-15due2026-05-01startlabel ← May 15
2due <- start + 143label <- format(due, "%b %d")4cat(label, "\n", sep = "")values this stepMay 15label2026-05-15duecat(label, " ", sep = "")
3label <- format(due, "%b %d")4cat(label, "\n", sep = "")outputMay 15values this stepMay 15label
Follow the Date
startis2026-05-01.- Adding
14days makesdueequal2026-05-15. format(due, "%b %d")turns the date intoMay 15.- The script prints
May 15. | name | value | | --- | --- | |start| 2026-05-01 | | days added | 14 | |due| 2026-05-15 | |label| May 15 |
as.Date
`as.Date` converts date text to a Date value.
date arithmetic
`start + 14` advances the date by fourteen days.
format
`format` turns a Date into display text.
Exercise: dates.R
Reproduce May 15, then trace how adding 14 days changes the start date.