Convert Date of Birth to Age in R (Example)

 

In this article, I’ll show how to convert a birthday date to the age of a person in R programming.

Table of contents:

Let’s start right away.

 

Creation of Example Data

We’ll use the data below as basement for this R programming tutorial:

x_birth <- as.Date("1980-03-27")    # Create date object for birthday
x_birth                             # Print birthday
# [1] "1980-03-27"

The previous output of the RStudio console shows the structure of the example data – It’s a single date object.

Note that we have created this date object using the as.Date function. This will be important for the conversion of this date later on.

 

Example: Change Birthday to Age Using age_calc() Function of eeptools Package

In this example, I’ll illustrate how to convert a birthday to an age variable.

First, we have to install and load the eeptools package:

install.packages("eeptools")        # Install eeptools package
library("eeptools")                 # Load eeptools package

Furthermore, we have to extract the current day using the Sys.Date function:

date_today <-  Sys.Date()           # Get current date
date_today                          # Print current date
# [1] "2021-09-02"

Now, we can apply the age_calc function of the eeptools package to our example date and the current date with the units argument set to “years”:

x_age <- age_calc(x_birth,          # Convert birth to age
                  date_today,
                  units = "years")
x_age                               # Print age with decimals
# [1] 41.43562

The previous output of the RStudio console shows the result of our R code: The age of the person corresponding to our example date is 41.43562 years.

In case we want to return the age as an integer value, we can modify our output using the floor function:

x_age_round <- floor(x_age)         # Round age to integer
x_age_round                         # Print rounded age
# [1] 41

The rounded age is 41.

 

Video, Further Resources & Summary

If you need further info on the R code of this tutorial, you might want to watch the following video on my YouTube channel. In the video, I explain the R syntax of this article.

 

The YouTube video will be added soon.

 

Besides that, you could read the other tutorials on https://statisticsglobe.com/.

 

In this post, I have demonstrated how to switch a birthday date to the age in the R programming language.

In the present example, we have converted a single date to an age variable. However, please note that we could apply the same syntax to a vector object or an entire column of a data frame as well.

Let me know in the comments, if you have any additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top