jeudi 15 décembre 2016

Date manipulation with lubridate and if-statement

I am struggling with some data manipulation. One of the columns in my datasheet contains the date of birth, but for one location the values are off by 100 years.

I made an example small data frame to explain my problem: the dates for Paris / Berlin are correct, I want to change the date only for those rows with London as location (for this example from 2028-3-25 to 1928-3-25).

library(lubridate)
date <- as.Date(c('1950-11-1','2028-3-25','1940-3-14'))
location <- c("Paris", "London", "Berlin")
df <- data.frame(date, location)
df$date_new <- ifelse(df$location %in% c("London"), df$date - years(100), df$date)

As you can see, I installed the lubridate package and tried to use an if else statement, but that just gives me some negative numbers in the new column.

The solution is probably very simple, but I cannot figure it out and it's driving me insane.

Thank you!

Aucun commentaire:

Enregistrer un commentaire