What does the error condition has length > 1 in R mean

if_else

#1

Hello,

I am trying to create buckets of age in R using:

if (insurance$AGE <= 30){
  insurance$AGE_GRP = 'Less than Middle Age'
}else if (insurance$AGE > 30 && insurance$AGE < 50){
  insurance$AGE_GRP = 'Middle'
}else insurance$AGE_GRP = '50 Plus'

However I am getting an error:

I understand that this is because each condition will generate more than one rows,but this is exactly what I want as in all the rows where the condition is satisfied I will assign an age group.
How do I resolve this??


#2

@data_hacks-I am not clear what is your data looks like but I suppose that you are accessing the whole column and you want to compare it with some value but the whole column can not be compared this way you have to access each element of column and then compare but in R by default it takes the first value.

Hope this helps,
Regards,
hinduja1234


#3

You should use the ifelse() statement. The following should work for the above as it will process the whole vector while if() only gives one output.

insurance$AGE_GRP = ifelse(insurance$AGE <= 30,‘Less than Middle Age’,ifelse(insurance$AGE > 30 && insurance$AGE < 50,‘Middle’,‘50 Plus’))

Hope this helps!
For more on ifelse(), see https://stat.ethz.ch/R-manual/R-devel/library/base/html/ifelse.html


#4

@data_hacks,

You can also perform the task without using if-else conditions

insurance$AGE_GRP = '50 Plus’
insurance$AGE_GRP[insurance$AGE<=30]='Less than Middle Age’
insurance$AGE_GRP[insurance$AGE > 30 && insurance$AGE < 50]=‘Middle’

Hope this helps!