epi_n00b epi_n00b - 2 months ago 6
R Question

Calculate person years by subtracting start date from end date

Suppose I have a dataframe that looks like this:

id start_date death_date
1 2011-05-20 2014-12-11
2 2014-08-01 2016-01-05
3 2005-01-02 2015-10-20
4 2015-06-30 2016-02-14
5 2014-07-01 2014-09-03


I want to create a new column that contains the difference between death_date and start_date in months UNLESS start_date is before 2014-05-31. If start_date < 2014-05-31, then I want the new column to be the difference between death_date and 2014-05-31 in months.

Code to create sample dataframe:

id <- c(1:5)
start_date <- c(as.Date("2011-05-20"), as.Date("2014-08-01"),
as.Date("2005-01-02"), as.Date("2015-06-30"),
as.Date("2014-07-01"))

death_date <- c(as.Date("2014-12-11"), as.Date("2016-01-05"),
as.Date("2015-10-20"), as.Date("2016-02-14"),
as.Date("2014-09-03"))

example_dates <- data.frame(id, start_date, death_date)

Answer

Try this:

df$new_col <- round(ifelse(df$start_date<as.Date("2014-05-31"), 
               df$death_date-as.Date("2014-05-31"), df$death_date-df$start_date)/30, 2)

  # id start_date death_date new_col
# 1  1 2011-05-20 2014-12-11    6.47
# 2  2 2014-08-01 2016-01-05   17.40
# 3  3 2005-01-02 2015-10-20   16.90
# 4  4 2015-06-30 2016-02-14    7.63
# 5  5 2014-07-01 2014-09-03    2.13