Home > Mobile >  recoding dates if larger than?
recoding dates if larger than?

Time:06-18

I have a few dates, and all that happened after 2005-06-12 should be recoded to 2005-06-12

# Example how the data looks like:
data <- data.frame(review_date = as.Date(c("2006-04-11","2001-01-01", "2005-06-12", "2007-07-09")))

Basically, desired output is that dates 2006-04-11 and 2007-07-09 become 2005-06-12. Thnx.

CodePudding user response:

We could use pmin

data$review_date <- pmin(data$review_date, "2005-06-12")
data$review_date
[1] "2005-06-12" "2001-01-01" "2005-06-12" "2005-06-12"

CodePudding user response:

We could do it with if_else:

library(dplyr)
data %>% 
  mutate(review_date = if_else(review_date > as.Date("2005-06-12"), as.Date("2005-06-12"), review_date))
  review_date
1  2005-06-12
2  2001-01-01
3  2005-06-12
4  2005-06-12
  • Related