Reputation: 25
So I have a data frame with multiple levels, shown below:
> levels
54 42 30 18 17 19 6 5 NA 78 4
I need to label these into 4 different groups
> labels
"0-1" "1-2" "2-3" "3+"
I'm trying to use the following code, but I need the levels to be the same length as the labels. How do I do that?
df$y <- cut(df$x, levels, labels)
Upvotes: 1
Views: 54
Reputation: 887421
May be cut
can be useful
cut(levels%/%12+1, breaks=c(-Inf, 1, 2, 3,Inf), labels = c("0-1", "1-2", "2-3", "3+"))
#[1] 3+ 3+ 2-3 1-2 1-2 1-2 0-1 0-1 <NA> 3+ 0-1
#Levels: 0-1 1-2 2-3 3+
levels <- c(54, 42, 30, 18, 17, 19, 6, 5, NA, 78, 4)
Upvotes: 1