0

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?

zx8754
  • 52,746
  • 12
  • 114
  • 209
cs0815
  • 16,751
  • 45
  • 136
  • 299

1 Answers1

1

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
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213