R函数 - 计算年龄组+空年龄

时间:2014-12-30 18:33:08

标签: r grouping categorization

尝试对年龄组进行分类,但可能只有Null年龄段。希望拥有这些团体" 0-4"," 5-24"," 25-49"," 50-64" "超过64" AND" Null Age"。

我是R的新手;试图改变别人的代码。

计算年龄组

以下是原始代码:

calculateAgeGroup<-function(this.age,this.age_units) {

  if(is.na(this.age) || is.na(this.age_units) || this.age=="NA") { return(NA) }

  # first of all, if age has a comma, take lower number
  this.minAge<-min(as.numeric(unlist(strsplit(this.age,","))))

  # calculate div factor for date unit
  this.divFactor = 1
  if (grepl("^y",this.age_units,ignore.case=TRUE,perl=TRUE)) { this.divFactor = 1 }
  if (grepl("^m",this.age_units,ignore.case=TRUE,perl=TRUE)) { this.divFactor = 12 }
  if (grepl("^d",this.age_units,ignore.case=TRUE,perl=TRUE)) { this.divFactor = 365 }

  this.yearsOfAge = this.minAge/this.divFactor

  # now calculate age group Age 0-4,5-24,25-49,50-64,over 64
  if (this.yearsOfAge < 5) { return("0-4") }
  if (this.yearsOfAge < 25) { return("5-24") }
  if (this.yearsOfAge < 50) { return ("25-49") }
  if (this.yearsOfAge < 65) { return ("50-64") }
  return("over 64")

 }

当它运行时,我收到以下错误:

  

if(this.yearsOfAge&lt; 5){:缺少值需要TRUE / FALSE

时出错      

另外:警告信息:

     

1:在mysqlExecStatement(conn,statement,...)中:     RS-DBI驱动程序警告:(作为字符导入的第1列中无法识别的MySQL字段类型7)

     

2:在函数中(this.age,this.age_units):强制引入的NA

1 个答案:

答案 0 :(得分:3)

可能有帮助

AgeGrp <- as.character(cut(v1, breaks=c(0,4,24,49,64,Inf),
          labels=c('0-4', '5-24', '25-49', '50-64', 'Over 64')))
AgeGrp[is.na(AgeGrp)] <- 'Null Age'

AgeGrp

数据

set.seed(39)
v1 <- sample(0:90, 40,replace=TRUE)
v1[5] <- NA