2014-12-20 227 views
1

我給出了客戶ID和他們的DOB。查找年齡和出生日期並按年齡分組R

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 

我想給組客戶在年齡(青少年:19-25等:13 -18青少年)提前

所有幫助表示感謝,謝謝!

+0

請準確定義的年齡組,並顯示出期望的結果 –

+1

@AJC你可以使用'cut' – akrun

回答

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')) 
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