vendredi 5 juin 2020

Issue with logical operators in R, using ifelse

Below is the code I'm using. The idea is that we have a variable "habitaciones" which counts the number of rooms in a hotel. I create a new variable called hrango, which has ranges based on the number of rooms. Hotels with 20 or less are small, between 21 and 40 are medium, and greater than 40 are large. You can see how I tried to process this using operators.

The problem when I run this code, is any hotel with less than 10 rooms is labeled as large, and any hotel with more than 100 rooms is labeled as small, and I cannot seem to figure out why. I started out using code based on "replace" which wasn't working properly, moved to ifelse, and still can't get the result I want.

Any help would be appreciated.

full_data <- full_data %>% 
  mutate (hrango = ifelse(habitaciones < 21, "Hoteles Pequenos",
                          ifelse(habitaciones > 20 & habitaciones < 41, "Hoteles Medios",
                                 ifelse(habitaciones > 40, "Hoteles Grandes", hrango)
                                        )
                                        )
                                       )

Aucun commentaire:

Enregistrer un commentaire