r - How do I use the lubridate package to calculate the number of months between two date vectors where one of the vectors has NA values? -
i've read lubridate package manual , have queried stack overflow variety of permutations of question have come no answer specific problem.
what i'm trying calculate age in months @ time of event difference between date of birth , specific event date.
as such, imported sas dataset using sas7bdat
package , converted sas date variables (dob , event) r objects using following code:
df$dob <- as.date(df$dob, origin="1960-01-01") df$dob1 <- ymd(df$dob)
and same thing event variable:
df$event <- as.date(df$event, origin="1960-01-01") df$event1 <- ymd(df$event)
however, there na values dob. so, following code want use calculate age (in months).
df$interval <- new_interval(df$dob1,df$event1) df$age1 <- df$interval %/% months(1)
i'm receiving error:
error in est[start + est * per < end] <- est[start + est * per < end] + : nas not allowed in subscripted assignments
what doing wrong? i've tried if/else function perhaps used incorrectly.
(note: sas programmers out there, i'm trying produce same results following function:
if dob ne . tage=floor(intck('month',dob,event)-(day(event)<day(dob)));
simple example using lubridate package
library(lubridate) date1='20160101' date2='20160501' x=interval(ymd(date1),ymd(date2)) x= x %/% months(1) print(x) # answer : 4
Comments
Post a Comment