This code creates quartiles named A, B, C and D.
x <- rnorm(100)
xcat <- factor(cut(x, quantile(x), include.lowest = TRUE),
labels = LETTERS[1:4])
Is there a way to name the quartiles Q1, Q2, Q3 and Q4 by adapting the above?
This code creates quartiles named A, B, C and D.
x <- rnorm(100)
xcat <- factor(cut(x, quantile(x), include.lowest = TRUE),
labels = LETTERS[1:4])
Is there a way to name the quartiles Q1, Q2, Q3 and Q4 by adapting the above?
We can use labels
argument to give it a name of our choice. Here, we use paste0
to generate levels as Q1, Q2...
set.seed(1)
x <- rnorm(10)
xcat <- factor(cut(x, quantile(x), include.lowest = TRUE),
labels = paste0("Q", 1:4))
xcat
#[1] Q1 Q2 Q1 Q4 Q3 Q1 Q3 Q4 Q4 Q2
#Levels: Q1 Q2 Q3 Q4