试图对年龄组进行分类,但可能有 Null 年龄。想要组“0-4”、“5-24”、“25-49”、“50-64”、“64 岁以上”和“空龄”。
我是 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 < 5) { 中的错误:需要 TRUE/FALSE 的缺失值
另外:警告信息:
1:在 mysqlExecStatement(conn, statement, ...) 中:RS-DBI 驱动程序警告:(作为字符导入的第 1 列中无法识别的 MySQL 字段类型 7)
2:在函数中(this.age,this.age_units):强制引入的NA