vendredi 15 novembre 2019

R: How to make a logical statement which finds a year between/in two date columns?

I have a problem executing something in R which maybe isn't hard but I simply can't figure it out.

Let's say I have the following dataframe with only two date columns: date_started and date_ended.

df <- data.frame(date_started=as.Date(c("1990-02-01","1995-03-04","1997-04-01","1999-01-11","1993-04-04")),
date_ended=as.Date(c("1993-08-12","1999-07-06","2000-06-05","1999-12-01","1996-07-08")))

They represent the start and end dates of the treatment of patients. Now I would like to add new columns which are either 1 (TRUE) or 0 (FALSE) when the person was treated in a certain year.

The result columns should be:

df$year_1990 <- c(1,0,0,0,0)
df$year_1991 <- c(1,0,0,0,0)
df$year_1992 <- c(1,0,0,0,0)
df$year_1993 <- c(1,0,0,0,1)
df$year_1994 <- c(0,0,0,0,1)
df$year_1995 <- c(0,1,0,0,1)
df$year_1996 <- c(0,1,0,0,1)
df$year_1997 <- c(0,1,1,0,0)
df$year_1998 <- c(0,1,1,0,0)
df$year_1999 <- c(0,1,1,1,0)
df$year_2000 <- c(0,0,1,0,0)

So I can count for each year how many people were treated.

I have tried and looked for a solution but simply can't find it. I've tried ifelse statements and the between function but I did not manage to solve this.

Any help is much appreciated!

Aucun commentaire:

Enregistrer un commentaire