1

I have a Dataframe with a date column and an outcome column (1 or 0):

df <- data.frame(matrix(nrow=100,ncol = 2))
colnames(df) <- c("date_1", "var")
df$date_1 <- sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 100)
df$var <- rbinom(100, 1, 0.2)

I want to make a new variable (preferably using mutate/dplyr) called "date_2" that assigns the date from the "date_1" variable but only if var==1?

krtbris
  • 344
  • 1
  • 9

1 Answers1

0

You can use case_when from dplyr

df %>%
   mutate(date_2 = case_when(var == 1 ~ date))

https://dplyr.tidyverse.org/reference/case_when.html

VvdL
  • 2,799
  • 1
  • 3
  • 14