I am trying to create a factor variable with 3 groups: 0, 1-2, and 3+.
I was using the cut function to make unequal breaks:
set.seed(2)
x <- rpois(n = 1000, lambda = 1.5)
breaks <- c(0, 1, 2, 3:max(x))
cut(x, breaks = breaks, right = TRUE)
## But this gives me the following:
Levels: (0,1] (1,2] (2,3] (3,8]
## I tried this as well:
breaks <- c(0, seq(1, 2, by = 1), 3:max(x))
## But that gives me:
Levels: (0,1] (1,2] (2,3] (3,4] (4,5] (5,6] (6,7] (7,8]
edit:
The following gives me the correct levels:
breaks <- c(0, 1, 3, Inf)
cut(x, breaks, right = FALSE, include.lowest = TRUE)
Levels: [0,1) [1,3) [3,Inf]
But I am getting an error message that my level [2] is duplicated:
x.factor <- factor(x, levels = cut(x, breaks = c(0, 1, 3, Inf), right = FALSE,
include.lowest = TRUE))
Not sure where to go from here.. any ideas?
Here, we can change the breaks with a large number at the end and specify the labels
grp <- cut(x, breaks = c(0:2, Inf), right = TRUE,
labels = c('0', '1-2', '3+'), include.lowest = TRUE)
levels(grp)
#[1] "0" "1-2" "3+"
head(x)
#[1] 0 2 2 0 4 4
head(grp)
#[1] 0 1-2 1-2 0 3+ 3+
#Levels: 0 1-2 3+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With