KT_1
KT_1

Reputation: 8474

Adding labels to cut function in R

For an example dataframe:

df1 <- structure(list(X = 1:15, a = c(2L, 3L, 4L, 3L, 7L, 5L, NA, 2L, 
9L, 7L, 0L, 1L, 20L, 15L, 14L)), .Names = c("X", "a"), 
   class = "data.frame", row.names = c(NA, 
-15L))

I am using the following code to divide column 'a' into quartiles:

cut.at.n.tile <- function(X , n = 4){ 
  cut( X , breaks = quantile( X , 
     probs = (0:n)/n , na.rm = TRUE ) , include.lowest = TRUE )}
df1$a.quartile <- cut.at.n.tile( df1$a , n = 4)

How do I replace the labels with 1 - 4 (1 being the lowest)? I don't wish to simply recode the values as I will be running this lots of times with different continuous variables.

Any help would be greatly appreciated.

Upvotes: 5

Views: 13322

Answers (2)

Kmcd39
Kmcd39

Reputation: 97

You could also just set labels to FALSE within the cut fcn; i.e.,

cut(df1$a,
    breaks = quantile( df1$a , 
                     probs = seq(0,1,.25) , na.rm = TRUE ),
    labels = F, # setting labels to false just makes labels integer codes instead of factor levels
    include.lowest = T)

Upvotes: 4

Ben Bolker
Ben Bolker

Reputation: 226057

Use the labels argument to cut ...

cut.at.n.tile <- function(X , n = 4){ 
   cut( X , breaks = quantile( X , 
      probs = (0:n)/n , na.rm = TRUE ) ,
      labels = 1:n,
      include.lowest = TRUE )}
cut.at.n.tile( df1$a , n = 4)
##  [1] 1    2    2    2    3    3    <NA> 1    4    3
##      1    1    4    4    4   
## Levels: 1 2 3 4

You might also be interested in ggplot2::cut_number, which does essentially the same thing ...

ggplot2::cut_number(df1$a, n = 4, labels = 1:4)

Upvotes: 7

Related Questions