I am using the lubridate
and dplyr
packages to work with date variables and to create a new date variable, respectively.
library(lubridate)
library(dplyr)
Let df
be my dataframe. I have two variables date1
and date2
. I want to create a new variable date
such that it takes the value of date1
. If date1
is missing, value of date2
is taken instead.
df <- data.frame(date1 = c("24/01/2016",NA,"22/07/2016"),
date2 = c("31/01/2016","09/02/2017",NA),
stringsAsFactors=FALSE)`
The above command gives:
date1 date2
1 24/01/2016 31/01/2016
2 <NA> 09/02/2017
3 22/07/2016 <NA>
I tried the following which I thought can give me the results desired. However, the new date
variables is in numerics.
df %>%
mutate_at(vars(date1,date2),dmy) %>%
mutate(date=ifelse(is.na(date1),date2,date1))
date1 date2 date
1 2016-01-24 2016-01-31 16824
2 <NA> 2017-02-09 17206
3 2016-07-22 <NA> 17004
I want:
date1 date2 date
1 2016-01-24 2016-01-31 2016-01-24
2 <NA> 2017-02-09 2017-02-09
3 2016-07-22 <NA> 2016-07-22
How do I solve this problem?