Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 441
Hendrik Avatar asked Mar 10 '23 09:03

Hendrik


1 Answers

Try this as an alternative

df$date_new <- df$date
df$date_new[df$location=="London"] <- df$date_new[df$location=="London"] - years(100)

or instead of df$date_new <- ifelse(df$location %in% c("London"), df$date - years(100), df$date)

try

df$date_new <- ifelse(df$location %in% c("London"), as.character(df$date - years(100)), as.character(df$date))
like image 189
dimitris_ps Avatar answered Mar 21 '23 05:03

dimitris_ps