Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change a column from birth date to age in r

Tags:

I am using data.table for the first time.

I have a column of about 400,000 ages in my table. I need to convert them from birth dates to ages.

What is the best way to do this?

like image 249
monkeyshines Avatar asked Nov 24 '14 01:11

monkeyshines


People also ask

How do I convert a date to age 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.

How is Lubridate used to calculate age?

Calculate age in R with lubridate Operator %–% creates a time interval from the date of the birth to a specified date, and that is divided with a 1-year period by using function years.

How do I convert a character to a date in R?

You can use the as. Date( ) function to convert character data to dates. The format is as. Date(x, "format"), where x is the character data and format gives the appropriate format.

How to calculate age in R?

The easiest and most accurate method to calculate age in R is by using the lubridate package. Operator %–% creates a time interval from the date of the birth to a specified date, and that is divided with a 1-year period by using function years. It is fas enough even in large datasets that have a couple of million age calculations.

How to create a date object for a birthday in R?

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.

How to convert birthdate to age without remembering formulas in Excel?

=DATEDIF (A2,TODAY (),"Y") & " Years, " & DATEDIF (A2,TODAY (),"YM") & " Months, " & DATEDIF (A2,TODAY (),"MD") & " Days" You can easily convert birthdate to age without remembering formulas with the Date & Time Helper of Kutools for Excel. Before applying Kutools for Excel, please download and install it firstly. 1.

How to calculate the age of a given column in Excel?

1. Supposing there are two columns containing birthdate and current date separately. 2. Select a blank cell to output the age, enter the below formula into it and press the Enter key. Select the result cell and then drag it's Fill Handle down to get all results.


1 Answers

I've been thinking about this and have been dissatisfied with the two answers so far. I like using lubridate, as @KFB did, but I also want things wrapped up nicely in a function, as in my answer using the eeptools package. So here's a wrapper function using the lubridate interval method with some nice options:

#' Calculate age
#' 
#' By default, calculates the typical "age in years", with a
#' \code{floor} applied so that you are, e.g., 5 years old from
#' 5th birthday through the day before your 6th birthday. Set
#' \code{floor = FALSE} to return decimal ages, and change \code{units}
#' for units other than years.
#' @param dob date-of-birth, the day to start calculating age.
#' @param age.day the date on which age is to be calculated.
#' @param units unit to measure age in. Defaults to \code{"years"}. Passed to \link{\code{duration}}.
#' @param floor boolean for whether or not to floor the result. Defaults to \code{TRUE}.
#' @return Age in \code{units}. Will be an integer if \code{floor = TRUE}.
#' @examples
#' my.dob <- as.Date('1983-10-20')
#' age(my.dob)
#' age(my.dob, units = "minutes")
#' age(my.dob, floor = FALSE)
age <- function(dob, age.day = today(), units = "years", floor = TRUE) {
    calc.age = interval(dob, age.day) / duration(num = 1, units = units)
    if (floor) return(as.integer(floor(calc.age)))
    return(calc.age)
}

Usage examples:

> my.dob <- as.Date('1983-10-20')

> age(my.dob)
[1] 31

> age(my.dob, floor = FALSE)
[1] 31.15616

> age(my.dob, units = "minutes")
[1] 16375680

> age(seq(my.dob, length.out = 6, by = "years"))
[1] 31 30 29 28 27 26
like image 52
Gregor Thomas Avatar answered Oct 02 '22 00:10

Gregor Thomas