Using one date as the base year

I'm trying to create a new date variable using lubridate package.
I used ifelse function to create a conditional command.

I want to make 2013 as the earliest entry date into the study. Some participants started ART much earlier than 2013. So I thought I those that started earlier should have their dates as 2013 while those that started later, there dates remain as is

I used

dat$entry_date=c(as_date(ifelse(start_date>1\1\2013,start_date, 1/1/2013)))

The result is the same as the original variable without applying the condition I set

One suggestion on how to achieve what you're after:

library(tidyverse)
library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
dates <- tibble(
  start_date = c(2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019)
)

dates %>% 
  mutate(start_date = lubridate::ymd(start_date, truncated =2L)) %>%
  mutate(entry_date = case_when(start_date < "2013-01-01" ~lubridate::ymd("2013", truncated = 2L),
                                TRUE ~ start_date))
#> # A tibble: 13 × 2
#>    start_date entry_date
#>    <date>     <date>    
#>  1 2007-01-01 2013-01-01
#>  2 2008-01-01 2013-01-01
#>  3 2009-01-01 2013-01-01
#>  4 2010-01-01 2013-01-01
#>  5 2011-01-01 2013-01-01
#>  6 2012-01-01 2013-01-01
#>  7 2013-01-01 2013-01-01
#>  8 2014-01-01 2014-01-01
#>  9 2015-01-01 2015-01-01
#> 10 2016-01-01 2016-01-01
#> 11 2017-01-01 2017-01-01
#> 12 2018-01-01 2018-01-01
#> 13 2019-01-01 2019-01-01

Created on 2021-10-20 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.