I have the following dataframe:
df <- data.frame(Business_Date = c("01/01/2018", "01/01/2019", "01/01/2020"), Business_Date_2019 = c("01/01/2019","01/01/2019","01/01/2019"), Television = c("A","B","C"))
And I would like to create a fourth column based which contains the Television for that specific date in 2019:
desired_output <- data.frame(Business_Date = c("01/01/2018", "01/01/2019", "01/01/2020"), Business_Date_2019 = c("01/01/2019","01/01/2019","01/01/2019"), Television = c("A","B","C"),
Television_2019 = c("B","B","B"))
However, when I write:
df$Television_2019 <- case_when(
df$Business_Date == df$Business_Date_2019 ~ df$Television,
TRUE ~ NA_character)
It doesn't return the desired_output:
Business_Date Business_Date_2019 Television Television_2019
1 01/01/2018 01/01/2019 A <NA>
2 01/01/2019 01/01/2019 B B
3 01/01/2020 01/01/2019 C <NA>
As row 1 and row 3 also have in the second column "01/01/2019", I would like to also return "B" in the fourth column for those rows.
How to write a script that returns the desired_output from df?