我获得了客户ID和他们的DOB。
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
我想根据年龄对顾客进行分组(青少年:13岁-18岁青少年:19-25岁等)
所有的帮助都表示赞赏,提前谢谢!
答案 0 :(得分:5)
尝试
indx <- round(as.numeric(difftime(Sys.Date(), df$DOB, unit='weeks'))/52.25)
df$grp <- cut(indx, breaks=c(0,13,18,25,Inf),
labels=c('kid', 'Teen', 'Young Adult', 'Old') )
答案 1 :(得分:3)
只是为了好玩,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