1

Hi everyone I am trying to assign categories to a range of values in a column that has a range of values from 365-433. I tried to use the case_when function and mimicked the syntax from the documentation as best I could but didn't seem to have the desired output. For clarification I am trying to designate data points with "Quadrant" values between 365-422 as "Transit", "Quadrant" values == to 424 as "ZOI", and "Quadrant" values == to 423 AND between 425-433 as "Adjacent". The last one is a bit tricky, because my focus is on that one 424 polygon, so if anyone has insight as to how I can account for that weird overlap in conditions I would so appreciate it.

Thanks!

sightingsData$quadID <- sightingsData$Quadrant
case_when(
  sightingsData$Quadrant %% 422 <= 0 ~ "Transit"
  sightingsData$Quadrant %% 424 == 0 ~ "ZOI"
  sightingsData$Quadrant %% 423 >= 0 ~ "Adjacent"
)

As you can see, my above code just essentially copied the values from the pre-existing "Quadrant" field to the new field "quadID" that I created. Not sure why exactly, but my understanding of the case_when function is also a bit hazy

  • Why are you using the modulus `%%` operator here? Don't you just want something like `case_when(sightingsData$Quadrant <= 422 ~ "Transit", sightingsData$Quadrant == 424 ~ "ZOI", sightingsData$Quadrant >= 423 ~ "Adjacent")`. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures of data aren't helpful because we can't copy/paste them into R for testing. – MrFlick Apr 13 '20 at 19:13
  • I'm not sure, I was really just following the syntax in the R help page. When I tried that it didn't work, and I got the same output, with these errors: Error: unexpected numeric constant in: "case_when( sightingsData$Quadrant 422" and so on for my remaining arguments – Salma Abdel-Raheem Apr 13 '20 at 19:17
  • I'd skip past the "fizz buzz" examples which do use modulus operators and instead look at the "star wars" examples to see perhaps more clear examples of how to use `case_when`. You just need to use value R expression that evaluates to a TRUE/FALSE value on the left side of the formula (`~`) and then the value you want on the right side. – MrFlick Apr 13 '20 at 19:18
  • Also, make sure to use `case_when` inside of a proper `mutate()` statement or be sure to save the results somewhere. Save it back to the data.frame after transformation rather than trying to change the class of the data afterward. – MrFlick Apr 13 '20 at 19:22

1 Answers1

0

If it is between, then we can change the code to

library(dplyr)
sightingsData <- sightingsData %>%
        mutate(quadID = case_when(Quadrant == 424 ~ "ZOI",
         Quadrant == 323 |between(Quadrant, 325, 433) ~ "Transit",

                   TRUE ~ "Adjacent"))

Also, for multiple groupings, an option is cut

sightingsData %>%
      mutate(quadID, cut(Quadrant, breaks = c(365, 422, 433, Inf), 
            labels = c("Transit", "ZOI", "Adjacent")))
akrun
  • 874,273
  • 37
  • 540
  • 662