年齢グループを分類しようとしていますが、Null 年齢がある可能性があります。グループ「0-4」、「5-24」、「25-49」、「50-64」、「64 歳以上」、および「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 < 5) { のエラー: TRUE/FALSE が必要な場所に値がありません
さらに: 警告メッセージ:
1: mysqlExecStatement(conn, statement, ...) 内: RS-DBI ドライバーの警告: (文字としてインポートされた列 1 の認識されない MySQL フィールド タイプ 7)
2: 関数内 (this.age, this.age_units) : 強制によって導入された NA