Skip to content

Instantly share code, notes, and snippets.

@ctesta01
Created December 9, 2020 22:46
Show Gist options
  • Select an option

  • Save ctesta01/e8ff705a4eef40533f0d21cc16f96b8e to your computer and use it in GitHub Desktop.

Select an option

Save ctesta01/e8ff705a4eef40533f0d21cc16f96b8e to your computer and use it in GitHub Desktop.
Creating Friendly Quantile Cut Labels in R
library(tidyverse)
library(magrittr)

# 1. here's some made up data 

df <- data.frame(
  value = rgamma(n=1000, 1, .1))

head(df)
##        value
## 1 41.6157399
## 2  5.2020570
## 3  0.8100238
## 4 12.3342646
## 5 17.8897725
## 6  4.4598388
# 2. add quantile groupings 

df %<>% mutate(
  quantile_group = cut(
    value,
    breaks = quantile(value, probs = seq(0,1,by=.2), na.rm=T) # na.rm=T is optional 
    ))

head(df)
##        value quantile_group
## 1 41.6157399    (16.2,73.9]
## 2  5.2020570    (2.24,5.28]
## 3  0.8100238 (0.00995,2.24]
## 4 12.3342646    (9.66,16.2]
## 5 17.8897725    (16.2,73.9]
## 6  4.4598388    (2.24,5.28]
# 3. pull out the quantile cut-points

df %<>% mutate(
  lower_cut = as.numeric( sub("\\((.+),.*", "\\1", quantile_group)),
  upper_cut = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", quantile_group)))

head(df)
##        value quantile_group lower_cut upper_cut
## 1 41.6157399    (16.2,73.9]  16.20000     73.90
## 2  5.2020570    (2.24,5.28]   2.24000      5.28
## 3  0.8100238 (0.00995,2.24]   0.00995      2.24
## 4 12.3342646    (9.66,16.2]   9.66000     16.20
## 5 17.8897725    (16.2,73.9]  16.20000     73.90
## 6  4.4598388    (2.24,5.28]   2.24000      5.28
# 4. infer ordering 

# convert the lower cut into an ordered factor and then into integers 
# reflecting that ordering.

df %<>% mutate(
  order = as.integer(as.ordered(lower_cut)))

head(df)
##        value quantile_group lower_cut upper_cut order
## 1 41.6157399    (16.2,73.9]  16.20000     73.90     5
## 2  5.2020570    (2.24,5.28]   2.24000      5.28     2
## 3  0.8100238 (0.00995,2.24]   0.00995      2.24     1
## 4 12.3342646    (9.66,16.2]   9.66000     16.20     4
## 5 17.8897725    (16.2,73.9]  16.20000     73.90     5
## 6  4.4598388    (2.24,5.28]   2.24000      5.28     2
# 5. create better labels

df %<>% mutate(
  friendly_label = factor(order, labels =
    c(`1` = "Lowest", 
      `2` = "2nd Quantile", 
      `3` = "3rd Quantile",
      `4` = "4th Quantile",
      `5` = "Highest")),
  friendly_informative_label = paste(
    friendly_label, quantile_group))

head(df)
##        value quantile_group lower_cut upper_cut order friendly_label friendly_informative_label
## 1 41.6157399    (16.2,73.9]  16.20000     73.90     5        Highest        Highest (16.2,73.9]
## 2  5.2020570    (2.24,5.28]   2.24000      5.28     2   2nd Quantile   2nd Quantile (2.24,5.28]
## 3  0.8100238 (0.00995,2.24]   0.00995      2.24     1         Lowest      Lowest (0.00995,2.24]
## 4 12.3342646    (9.66,16.2]   9.66000     16.20     4   4th Quantile   4th Quantile (9.66,16.2]
## 5 17.8897725    (16.2,73.9]  16.20000     73.90     5        Highest        Highest (16.2,73.9]
## 6  4.4598388    (2.24,5.28]   2.24000      5.28     2   2nd Quantile   2nd Quantile (2.24,5.28]
library(tidyverse)
library(magrittr)
# 1. here's some made up data
df <- data.frame(
value = rgamma(n=1000, 1, .1))
head(df)
# 2. add quantile groupings
df %<>% mutate(
quantile_group = cut(
value,
breaks = quantile(value, probs = seq(0,1,by=.2), na.rm=T) # na.rm=T is optional
))
head(df)
# 3. pull out the quantile cut-points
df %<>% mutate(
lower_cut = as.numeric( sub("\\((.+),.*", "\\1", quantile_group)),
upper_cut = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", quantile_group)))
head(df)
# 4. infer ordering
# convert the lower cut into an ordered factor and then into integers
# reflecting that ordering.
df %<>% mutate(
order = as.integer(as.ordered(lower_cut)))
head(df)
# 5. create better labels
df %<>% mutate(
friendly_label = factor(order, labels =
c(`1` = "Lowest",
`2` = "2nd Quantile",
`3` = "3rd Quantile",
`4` = "4th Quantile",
`5` = "Highest")),
friendly_informative_label = paste(
friendly_label, quantile_group))
head(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment