日付の扱いは Tidyverse パッケージ群の一つ lubridate を使うと超絶ラクになります。
pacman::p_load(tidyverse, lubridate)
> lubridate::days_in_month(as_date("2021-1-1"))
Jan
31
大量データ処理に便利な関数が山盛りですので、ぜひ慣れて見てください。
> p_funs(lubridate)
[1] ".__C__Duration" ".__C__Interval" ".__C__Period" ".__C__Timespan"
[5] ".__T__-:base" ".__T__[:base" ".__T__[[:base" ".__T__[[<-:base"
[9] ".__T__[<-:base" ".__T__*:base" ".__T__/:base" ".__T__%/%:base"
[13] ".__T__%%:base" ".__T__%m-%:lubridate" ".__T__%m+%:lubridate" ".__T__%within%:lubridate"
[17] ".__T__+:base" ".__T__$:base" ".__T__$<-:base" ".__T__Arith:base"
[21] ".__T__as_date:lubridate" ".__T__as_datetime:lubridate" ".__T__as.character:base" ".__T__as.difftime:base"
[25] ".__T__as.duration:lubridate" ".__T__as.interval:lubridate" ".__T__as.numeric:base" ".__T__as.period:lubridate"
[29] ".__T__c:base" ".__T__Compare:methods" ".__T__date<-:lubridate" ".__T__day<-:lubridate"
[33] ".__T__format_ISO8601:lubridate" ".__T__hour<-:lubridate" ".__T__minute<-:lubridate" ".__T__month<-:lubridate"
[37] ".__T__qday<-:lubridate" ".__T__reclass_timespan:lubridate" ".__T__rep:base" ".__T__second<-:lubridate"
[41] ".__T__show:methods" ".__T__time_length:lubridate" ".__T__year<-:lubridate" "%--%"
[45] "%m-%" "%m+%" "%within%" "add_with_rollback"
[49] "am" "Arith" "as_date" "as_datetime"
[53] "as.difftime" "as.duration" "as.interval" "as.period"
[57] "ceiling_date" "Compare" "cyclic_encoding" "date"
[61] "Date" "date_decimal" "date<-" "day"
[65] "day<-" "days" "days_in_month" "ddays"
[69] "decimal_date" "dhours" "dmicroseconds" "dmilliseconds"
[73] "dminutes" "dmonths" "dmy" "dmy_h"
[77] "dmy_hm" "dmy_hms" "dnanoseconds" "dpicoseconds"
[81] "dseconds" "dst" "duration" "dweeks"
[85] "dyears" "dym" "epiweek" "epiyear"
[89] "fast_strptime" "fit_to_timeline" "floor_date" "force_tz"
[93] "force_tzs" "format_ISO8601" "guess_formats" "hm"
[97] "hms" "hour" "hour<-" "hours"
[101] "int_aligns" "int_diff" "int_end" "int_end<-"
[105] "int_flip" "int_length" "int_overlaps" "int_shift"
[109] "int_standardize" "int_start" "int_start<-" "intersect"
[113] "interval" "is.Date" "is.difftime" "is.duration"
[117] "is.instant" "is.interval" "is.period" "is.POSIXct"
[121] "is.POSIXlt" "is.POSIXt" "is.timepoint" "is.timespan"
[125] "isoweek" "isoyear" "leap_year" "local_time"
[129] "make_date" "make_datetime" "make_difftime" "mday"
[133] "mday<-" "mdy" "mdy_h" "mdy_hm"
[137] "mdy_hms" "microseconds" "milliseconds" "minute"
[141] "minute<-" "minutes" "month" "month<-"
[145] "ms" "my" "myd" "NA_Date_"
[149] "NA_POSIXct_" "nanoseconds" "now" "origin"
[153] "parse_date_time" "parse_date_time2" "period" "period_to_seconds"
[157] "picoseconds" "pm" "POSIXct" "pretty_dates"
[161] "qday" "qday<-" "quarter" "reclass_date"
[165] "reclass_timespan" "rollback" "rollbackward" "rollforward"
[169] "round_date" "second" "second<-" "seconds"
[173] "seconds_to_period" "semester" "setdiff" "show"
[177] "stamp" "stamp_date" "stamp_time" "time_length"
[181] "today" "tz" "tz<-" "union"
[185] "wday" "wday<-" "week" "week<-"
[189] "weeks" "with_tz" "yday" "yday<-"
[193] "ydm" "ydm_h" "ydm_hm" "ydm_hms"
[197] "year" "year<-" "years" "ym"
[201] "ymd" "ymd_h" "ymd_hm" "ymd_hms"
[205] "yq"