-2

I'm having some trouble finding out how to do a specific thing in R.

In my dataset, I have a column with the date of birth of participants. I also have a column giving me the age in days at which a disease was diagnosed.

What I want to do is to create a new column showing the date of diagnosis. I'm guessing it's a pretty easy thing to do since I have all the information needed, basically it's birth date + X number of days = Date of diagnosis, but I'm unable to figure out how to do it.

All of my searches give me information on the opposite, going from date to age. So if you're able to help me, it would be much appreciated!

floubert
  • 3
  • 1
  • 1
    Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including the code you tried and a snippet of your data or some fake data. – stefan Jun 29 '22 at 22:29
  • 1
    `birth_date + age` as you describe should do it: `as.Date("2000-00-01") + 50` for example works because an R `Date` is just stored as the number of days since 1970-01-01. – thelatemail Jun 29 '22 at 22:29
  • Possible duplicate of https://stackoverflow.com/questions/2254986/how-to-subtract-add-days-from-to-a-date – thelatemail Jun 29 '22 at 22:32
  • `lubridate` `days` – TTS Jun 29 '22 at 23:00
  • @stefan sorry, it's my first post I didn't know, I'll keep it in mind for the next ones! – floubert Jun 30 '22 at 15:15

1 Answers1

0
library(tidyverse)
library(lubridate)

df <- tibble(
  birth = sample(seq("1950-01-01" %>% 
                as.Date(), 
              today(), by = "day"), 10, replace = TRUE), 
  age = sample(3650:15000, 10, replace = TRUE)
)

df %>%  
  mutate(diagnosis_date = birth %m+% days(age))

#> # A tibble: 10 x 3
#>    birth        age diagnosis_date
#>    <date>     <int> <date>        
#>  1 1955-01-16  6684 1973-05-05    
#>  2 1958-11-03  6322 1976-02-24    
#>  3 2007-02-23  4312 2018-12-14    
#>  4 2002-07-11  8681 2026-04-17    
#>  5 2021-12-28 11892 2054-07-20    
#>  6 2017-07-31  3872 2028-03-07    
#>  7 1995-06-30 14549 2035-04-30    
#>  8 1955-09-02 12633 1990-04-04    
#>  9 1958-10-10  4534 1971-03-10    
#> 10 1980-12-05  6893 1999-10-20

Created on 2022-06-30 by the reprex package (v2.0.1)

Chamkrai
  • 5,912
  • 1
  • 4
  • 14