3

Calculate age from given column birth_date and specified date "2017-01-01" with format "%y-%m-%d" in R.

name birth_date
 a    1964-09-01
 b    1977-01-02
 c    1977-01-04
 d    1967-01-02
 e    1977-04-02
 f    1945-01-02

 x <- data.frame("name" =c("a","b","c","d","e","f"), "birth_date" = c("1964-09-01","1977-01-02","1977-01-04","1967-01-02","1977-04-02","1945-01-02"))
x

difftime(as.Date(x$birth_date),as.Date(2017-01-01), units = "years")
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66

4 Answers4

3

The following function needs package lubridate.
It has 2 arguments:

  1. A vector of dates.
  2. A base date, from which to compute the age. Defaults to the current system date.

The first call uses the default base. The second call passes the base date in the question as the second argument.

library(lubridate)

age <- function(birth, base = Sys.Date()){
  i <- interval(birth, base)
  p <- as.period(i)
  year(p)
}

age(x$birth_date)
#[1] 54 42 42 52 42 74

age(x$birth_date, "2017-01-01")
#[1] 52 39 39 49 39 71
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
1

There is no "years" unit in difftime, the biggest one is "weeks".

One way would be to calculate difftime in "days" and then divide by 365.2422 to get difference in years.

as.numeric(difftime(as.Date("2017-01-01"),as.Date(x$birth_date), units = "days")/365.2422)
#[1] 52.33514 39.99812 39.99264 49.99970 39.75170 71.99880

You could also use any other combination say select units as "weeks" and then convert it into equivalent "year".

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

You can do it easily using lubridate

x <- data.frame("name" =c("a","b","c","d","e","f"), "birth_date" = c("1964-09-01","1977-01-02","1977-01-04","1967-01-02","1977-04-02","1945-01-02"))
x

library(lubridate)

x$birth_date <- ymd(x$birth_date)

x$age <- year(Sys.Date()) - year(x$birth_date)

Hope its simple and helps

Hunaidkhan
  • 1,411
  • 2
  • 11
  • 21
0

There are a couple of problems in your code. First, your date (2017-01-01) needs to be a string in quotes ('2017-01-01'). Second, you probably want to reverse the order of your dates. Third, difftime does not have a 'years' units. Without bringing in other packages or methods, here's the best approximation you can get for birthday age in years.

x <- data.frame("name" =c("a","b","c","d","e","f"), 
                "birth_date" = c("1964-09-01","1977-01-02","1977-01-04","1967-01-02","1977-04-02","1945-01-02"))

as.numeric(difftime(as.Date('2017-01-01'), as.Date(x$birth_date), units = "days")/365.2422)
Todd Burus
  • 963
  • 1
  • 6
  • 20