Home > Software engineering >  Collapsing a percentage into ordinal categories using ifelse() in R?
Collapsing a percentage into ordinal categories using ifelse() in R?

Time:12-17

I've been using R for awhile now and have computed variables before but this takes the cake. I've followed tutorials using mutate and ifelse to collapse my continuous percent variable into ordinal categories and I just can't figure out why all my values are returning 0. Can anyone tell me what I'm missing? Here's some sample code:

SampleData <- data.frame(Percent = c(0.0, 0.20, 0.25, 0.26, 0.30, 0.50, 0.51, 0.60, 0.76, 0.80, 1.0))


SampleData$PercentOrdinal <- as.numeric(SampleData$Percent)
SampleData$PercentOrdinal <- as.numeric(SampleData$PercentOrdinal)
SampleData$PercentOrdinal = ifelse(SampleData$Percent == 0, 0, SampleData$PercentOrdinal)
SampleData$PercentOrdinal = ifelse(SampleData$Percent > 0 && SampleData$Percent <= 0.2599936, 0, SampleData$PercentOrdinal)  #What is wrong here? It makes all values 0. 
SampleData$PercentOrdinal = ifelse(SampleData$Percent >= 0.26 && SampleData$Percent <= 0.5099602, 1, SampleData$PercentOrdinal)
SampleData$PercentOrdinal = ifelse(SampleData$Percent >= 0.52 && SampleData$Percent <= 0.7599730, 2, SampleData$PercentOrdinal)
SampleData$PercentOrdinal = ifelse(SampleData$Percent >= 0.77 && SampleData$Percent <= 1.0, 3, SampleData$PercentOrdinal)

summary(SampleData$PercentOrdinal)
table(SampleData$PercentOrdinal)

CodePudding user response:

A few things to suggest here:

  • & does the comparisons you want and almost completes your code
  • however, from the above the values 0.51 and 0.76 aren't included in any of your categories. Using <= and > for cut values in subsequent lines is probably what you mean to do.
  • cut as suggested above would likely make this neater and easier to manage:
SampleData <-
  data.frame(Percent = c(0.0, 0.20, 0.25, 0.26, 0.30, 0.50, 0.51, 0.60, 0.76, 0.80, 1.0))

SampleData$PercentOrdinal <-
  cut(
    SampleData$Percent,
    breaks = c(0, 0.26, 0.52, 0.77, 1),
    include.lowest = TRUE,
    labels = FALSE, 
    right = FALSE 
  ) - 1


SampleData
#>    Percent PercentOrdinal
#> 1     0.00              0
#> 2     0.20              0
#> 3     0.25              0
#> 4     0.26              1
#> 5     0.30              1
#> 6     0.50              1
#> 7     0.51              2
#> 8     0.60              2
#> 9     0.76              3
#> 10    0.80              3
#> 11    1.00              3

Created on 2021-12-17 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related