Home > Software design >  Replacing numerical values into categorical values in R
Replacing numerical values into categorical values in R

Time:05-12

I am trying to substitute values in the following range but I don't understand why for the 2 first variables, the second substitution is not working but the third variable does. Any idea to fix it?

This is my code:

# Creating new variables:
df[, "icPM10"] <- df$PM10
df[, "icO3"] <- df$O3
df[, "RS_re"] <- df$RS

# Substitution

df$icPM10[df$icPM10 > 0 & df$icPM10 <= 45 ] <- "aceptable"
df$icPM10[df$icPM10 > 45 & df$icPM10 <= 180  ] <- "mejorable"

df$icO3[df$icO3 > 0 & df$icO3 <= 60 ] <- "aceptable"
df$icO3[df$icO3 > 60 & df$icO3 <= 170  ] <- "mejorable"


df$RS_re[df$RS_re > 0 & df$RS_re <= 100 ] <- "normal_baja"
df$RS_re[df$RS_re > 100 & df$RS_re <= 700  ] <- "normal_alta"

And this is the output (sample):

enter image description here

I did this kind of substitution in other datasets and had no problem. At first, I thought it could be because I transformed the column into a char, but it doesn't really matter.

Any help will be welcome!

CodePudding user response:

After the first assignment, icPM10 column is no longer numeric as it gets converted to character class and thus the second expression on the same column wouldn't work. We may use ifelse or case_when (would be better as it can take multiple expressions)

library(dplyr)
df <- df %>%
      mutate(icPM10 = case_when(between(icPM10, 0, 45) ~ 'acetable',
                                between(icPM10, 45, 180) ~ 'mejorable',
                        TRUE ~ as.character(icPM10)))
  • Related