ifelse logic issue

Hi, why does my final ifelse fail with both Ed and Fred?

People <- c("Ann","Bill","Cathy","Dick","Ed","Fred")
Age <- c(20,30,40,50,80,NA)
df <- data.frame(cbind(People,Age))
df
df$Age <- ifelse(df$Age <= 29, "18-29",
ifelse(df$Age >= 30 & df$Age <= 44, "30-44",
ifelse(df$Age >= 45 & df$Age <= 59, "45-59",
ifelse(df$Age >= 60 & df$Age <= 74, "60-74",
ifelse(df$Age >= 75 & df$Age <= 120, "75+ ", "Unknown")))))
df

People Age
1 Ann 20
2 Bill 30
3 Cathy 40
4 Dick 50
5 Ed 80
6 Fred

People Age
1 Ann 18-29
2 Bill 30-44
3 Cathy 30-44
4 Dick 45-59
5 Ed Unknown
6 Fred

This is a cbind gotcha.

The type of a matrix result determined from the highest type of any of the inputs in the hierarchy raw < logical < integer < double < complex < character < list .

In other words, if the vectors being combined are not all of the same type, they will default to the least restrictive type—character.

People <- c("Ann","Bill","Cathy","Dick","Ed","Fred")
Age <- c(20,30,40,50,80,NA)
dat <- data.frame(People = People,Age = Age)
dat
#>   People Age
#> 1    Ann  20
#> 2   Bill  30
#> 3  Cathy  40
#> 4   Dick  50
#> 5     Ed  80
#> 6   Fred  NA
dat$Age <- ifelse(dat$Age <= 29, "18-29",
                 ifelse(dat$Age >= 30 & dat$Age <= 44, "30-44",
                        ifelse(dat$Age >= 45 & dat$Age <= 59, "45-59",
                               ifelse(dat$Age >= 60 & dat$Age <= 74, "60-74",
                                      ifelse(dat$Age >= 75 & dat$Age <= 120, "75+ ", "Unknown")))))
dat
#>   People   Age
#> 1    Ann 18-29
#> 2   Bill 30-44
#> 3  Cathy 30-44
#> 4   Dick 45-59
#> 5     Ed  75+ 
#> 6   Fred  <NA>
1 Like

Nice find about cbind. Totally missed it until saw the answer.

In case of confusion regarding why others worked, that's almost by accident. They are being compared as strings only, and that means it's being done lexicographically.

All of those comparisons continue to give same result as numeric comparisons, until number if digits increased and "80" was being compared to 120 (automatically coerced to "120").

Hope this helps.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.