I have a heterogeneous data like date are in POISXct format
ID birth new_birth
1 1990-10-16 NA
2 1883-12-31 23:50:39 1983-12-31
3 1945-01-16 00:00:00 NA
I want to replace birth if there are something in new_birth and birth<1910 I change birth & new_birth in Date time with as.Date I want to create and new columns Year with
df <- df %>%
mutate(Year = as.date(birth, format="Y")
but Year stay at format YMD, why ? and after I want to do ifelse to have finally
ID birth new_birth
1 1990-10-16 NA
2 1983-12-31 1983-12-31
3 1945-01-16 NA
Do you have any idea to help me ?
You can use parse_date_time
to change date and time in different formats to POSIXct
class. If birth year is less than 1910 then replace birth
value with new_birth
.
library(dplyr)
library(lubridate)
df %>%
mutate(birth = as.Date(parse_date_time(birth, c('Ymd', 'YmdHMS'))),
new_birth = as.Date(new_birth),
birth = if_else(year(birth) < 1910, new_birth, birth))
# ID birth new_birth
#1 1 1990-10-16 <NA>
#2 2 1983-12-31 1983-12-31
#3 3 1945-01-16 <NA>
data
df <- structure(list(ID = 1:3, birth = c("1990-10-16", "1883-12-31 23:50:39",
"1945-01-16 00:00:00"), new_birth = c(NA, "1983-12-31", NA)),
class = "data.frame", row.names = c(NA, -3L))