0
votes

I was trying to execute this functions, but I kept on getting an error with my if statment:

Error in if (unique(AGE$age[AGE$age[i]] > 60)) { : missing value where TRUE/FALSE needed

y<-NULL
for(i in unique(infofile$family)){
  AGE<-infofile[infofile$family==i,]
  if(unique(AGE$age[i]> 60)){
    AGE$yearsold[i]<-"OLD"
  }else{AGE$yearsold[i]<-"YOUNG"}
  y<-rbind(y,AGE)
}
2
Try unique(AGE$age[i])> 60 instead - karen
Probably there is na value for AGE$age or unique(AGE$age[i]> 60 condition doesn't match any row. - MKR
I just tried your advise, but I still can not solve the problem. - Amber Tseng
Please update your question by posting reproducible errors (sample data), so that it anyone can easily respond. Thanks - Joy
I have used is.na(unique(AGE$age[i]) to check, but there is not any NA. - Amber Tseng

2 Answers

0
votes

If your objective is to create a new column based on the condition on age then you can do it without using a for loop. Try the code below:

AGE$yearsold <- ifelse(AGE$age>60, "OLD", "YOUNG")
0
votes

Oh I see, I think what you want is:

sum(AGE$age<=60, na.rm = TRUE) < 1

If TRUE then is like 1 and if FALSE is like 0