Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

add column age from column birthdate in R

Name Date
A 1990-10-7
B 1997-11-20

and i want to add column age the convert the date to the age

i try this

data$age <- age_calc(as.Date(data$dob, "%Y/%m/%d"), units = "years")

but i got this error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Error in if (any(enddate < dob)) { : 
  missing value where TRUE/FALSE needed

>Solution :

The error is in the format you are giving to as.Date function

> df$age <- age_calc(as.Date(df$Date, "%Y-%m-%d"), units = "years")
> df
  Name       Date      age
1    A  1990-10-7 32.15342
2    B 1997-11-20 25.03288

Note that your Date variable use - as separator instead of / so you have to use - inside as.Date

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading