AdamO
AdamO

Reputation: 4960

How to find maximum possible date in a month in R?

We have a questionnaire with some "fuzzy" items regarding the self-report time of some events. I would like to create a minimum and maximum possible dates for those times. Usually year and month are given, but day is missing. As such the minimum day works for every month since there is always a 1, but the max throws an error. Aside from trial and error, is there an easy way to look up how many days were in a month for a given year?

year <- c(2016, 2017)
month <- c(2, 2)
day <- c(29, 29)

as.Date(paste(year, month, day, sep='-'))

gives

> as.Date(paste(year, month, day, sep='-'))
[1] "2016-02-29" NA  

but I want:

myDate(paste(year, month, day, sep='-'))
[1] "2016-02-29" "2016-02-28"

Upvotes: 1

Views: 402

Answers (2)

dww
dww

Reputation: 31454

We can subtract 1 day from the first of the following month

year.dat <- c(2016, 2017)
month.dat <- c(2, 2)
day.dat <- c(29, 29)

as.Date(paste (1, month.dat+1, year.dat, sep='-'), format = '%d-%m-%Y') - 1
#[1] "2016-02-29" "2017-02-28"

Note that this naiive version fails when the month is december, because as.Date then returns NA when we try to add one to the month. To accomodate rolling december over to january of the next year we can improve this thusly:

as.Date(paste (1, c(1:12, 1)[month.dat+1], year.dat + (month.dat==12), sep='-'), format = '%d-%m-%Y') - 1

Upvotes: 1

You can work with the "aggregate" function:

maxdaysbymonth <- aggregate(day ~ month, df, max)
mindaysbymonth <- aggregate(day ~ month, df, min)

Also you can extend that for years:

maxdaysbymonthbyyear <- aggregate(day ~ month + year, df, max)
mindaysbymonthbyyear <- aggregate(day ~ month + year, df, min)

Upvotes: 0

Related Questions