只是爲了好玩,一個dplyr
版本計算年略有不同,並使用unclass
代替as.numeric
:
library(dplyr)
data <- read.table(text="CUSTOMER.ID DOB
111111 1992-01-09
222222 1999-02-20
333333 1997-03-07
444444 1969-10-15
555555 1992-11-12
666666 1939-09-03", stringsAsFactors=FALSE, header=TRUE)
wk_yr <- 0.0191653490489196 # fraction of a year that a week is
data %>% mutate(AGE=(wk_yr * difftime(Sys.Date(),
as.Date(DOB),
units="weeks") %>% unclass) %>% round,
GROUP=cut(AGE,
breaks=c(0, 13, 18, 25, Inf),
labels=c('kid', 'Teen', 'Young Adult', 'Old')))
## CUSTOMER.ID DOB AGE GROUP
## 1 111111 1992-01-09 23 Young Adult
## 2 222222 1999-02-20 16 Teen
## 3 333333 1997-03-07 18 Teen
## 4 444444 1969-10-15 45 Old
## 5 555555 1992-11-12 22 Young Adult
## 6 666666 1939-09-03 75 Old
請準確定義的年齡組,並顯示出期望的結果 –
@AJC你可以使用'cut' – akrun