Get Age from date of birth in R

In Order to get age from date of birth in R we will be using difftime () function. Let’s see how to get age from date of birth in R with an example.

Let’s first create the dataframe.

### Create Data Frame
df1 = data.frame (
  Name =c('Annie','Catherine','Teresa','Peterson','Richard','joe'),
  Date_of_birth = as.Date(c('1995-06-16','1991-04-19','1993-07-22','1990-03-26','1991-05-12','1992-09-13')))
df1

dataframe df1 will be

Get age from date of birth in R 1

 

Get age from date of birth in R

Age is extracted from Date_of_birth column using difftime() functions in roundabout way by extracting the number of weeks between date of birth and current date and dividing by 52.25, as shown below.

# age from dob
df1$Current_age = as.numeric(difftime(Sys.Date(),df1$Date_of_birth, units = "weeks"))/52.25
df1

So the resultant data frame has a column current_age with age extracted from date of birth

Get age from date of birth in R 2