Calculate ages in R

前端 未结 8 1826
北荒
北荒 2020-11-27 16:39

I have two data frames in R. One frame has a persons year of birth:

YEAR
/1931
/1924

and then another column shows a more recent time.

8条回答
  •  爱一瞬间的悲伤
    2020-11-27 17:20

    Given the data in your example:

    > m <- data.frame(YEAR=c("/1931", "/1924"),RECENT=c("09/08/2005","11/08/2005"))
    > m
       YEAR     RECENT
    1 /1931 09/08/2005
    2 /1924 11/08/2005
    

    Extract year with the strptime function:

    > strptime(m[,2], format = "%m/%d/%Y")$year - strptime(m[,1], format = "/%Y")$year
    [1] 74 81
    

提交回复
热议问题