The cut()
function converts numerical variables to factors. You can supply breaks
to tell where the cuts should happen. This replaces your attempt at levels
. Then you apply your labels
. You also need to specify a right
argument - should the interval be closed on the right (or left if false).
set.seed(10)
x <- sample(1:200, 1000, replace = TRUE)
summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
1.00 50.75 101.00 101.57 153.00 200.00
x <- cut(x, breaks = c(0, 10, 49, 200),
labels = c("small", "medium", "large"),
right = TRUE)
summary(x)
small medium large
51 189 760
I also want to point out an issue with your code. In your line labels = c("small"),c("medium"),c("large")
, you have the commas outside the c()
. You should enclose all desired elements of your vector in the same c()
:
labels = c("small", "medium", "large")
If the commas are outside of the parentheses, R will map only c("small")
to labels
and then try to match c("medium")
to the next argument of the function.