I want to create a new variable with 3 arbitrary categories based on continuous data.
set.seed(123)
df <- data.frame(a = rnorm(100))
Using base I would
df$category[df$a < 0.5] <- "low"
df$category[df$a > 0.5 & df$a < 0.6] <- "middle"
df$category[df$a > 0.6] <- "high"
Is there a dplyr, I guess mutate()
, solution for this?
Furthermore, is there a way to calculate the categories rather than choosing them? I.e. let R calculate where the breaks for the categories should be.
EDIT
The answer is in this thread, however, it does not involve labelling, which confused me (and may confuse others) therefore I think this question serves a purpose.