user12388610
user12388610

Reputation: 207

In R, how can I calculate age based on birth date using eeptools?

I am trying to calculate the age based on birth date using eeptools in R.

I follow some tutorial pages but it didn't work

My raw dataset is like below :

   Born       bioguide   
1946-05-27    A000370 
1979-06-19    A000371 
1980-04-18    A000367 
1958-06-12    A000369 
1948-03-23    B001291 
1973-07-24    B000213 
1949-09-15    B001281 
1950-03-12    B001271 
1952-04-20    B001292 
1950-06-20    B001293

And also my desired output is (Age may be little wrong since I calculate manually):

   Born        Age   
1946-05-27     72 
1979-06-19     41 
1980-04-18     40 
1958-06-12     62 
1948-03-23     72 
1973-07-24     47 
1949-09-15     71 
1950-03-12     70 
1952-04-20     68 
1950-06-20     70

Could you please help me to make the desired output?

Many thanks,

Upvotes: 0

Views: 416

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 102349

You can use package lubridate to calculate the age via time_length() + difftime()

library(lubridate)
df <- within(df,age <- round(time_length(difftime(Sys.Date(),as.Date(Born)),"years")))

such that

> df
         Born bioguide age
1  1946-05-27  A000370  74
2  1979-06-19  A000371  41
3  1980-04-18  A000367  40
4  1958-06-12  A000369  62
5  1948-03-23  B001291  72
6  1973-07-24  B000213  46
7  1949-09-15  B001281  70
8  1950-03-12  B001271  70
9  1952-04-20  B001292  68
10 1950-06-20  B001293  70

Upvotes: 1

mrhellmann
mrhellmann

Reputation: 5529

It looks like eeptools has an age_calc() function.

your_data <- data.frame(stringsAsFactors=FALSE,
        Born = c("1946-05-27", "1979-06-19", "1980-04-18", "1958-06-12",
                 "1948-03-23", "1973-07-24", "1949-09-15", "1950-03-12",
                 "1952-04-20", "1950-06-20"),
    bioguide = c("A000370", "A000371", "A000367", "A000369", "B001291",
                 "B000213", "B001281", "B001271", "B001292", "B001293")
)

library(eeptools)
#> Loading required package: ggplot2

your_data$age <- eeptools::age_calc(dob = as.Date(your_data$Born),
                                    enddate = Sys.Date(),
                                    units = 'years')

your_data
#>          Born bioguide      age
#> 1  1946-05-27  A000370 73.62459
#> 2  1979-06-19  A000371 40.56158
#> 3  1980-04-18  A000367 39.73224
#> 4  1958-06-12  A000369 61.58075
#> 5  1948-03-23  B001291 71.80328
#> 6  1973-07-24  B000213 46.46569
#> 7  1949-09-15  B001281 70.32048
#> 8  1950-03-12  B001271 69.83281
#> 9  1952-04-20  B001292 67.72678
#> 10 1950-06-20  B001293 69.55884

Created on 2020-01-10 by the reprex package (v0.3.0)

More on eeptools here: https://github.com/jknowles/eeptools

Upvotes: 2

jyr
jyr

Reputation: 692

Not sure how to do it with eptools, but it should be simple enough without it.

dta <- data.frame(born = as.Date(c("1934-02-02", "1956-02-05"),
                  id=c("A","B")))
dta$age <-  as.numeric(round((as.Date("2020-01-01")-dta$born)/365))

Upvotes: 1

Related Questions