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!
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With