Skip to content

Instantly share code, notes, and snippets.

@rafapereirabr
Last active July 23, 2020 15:31
Show Gist options
  • Save rafapereirabr/c2299b650f0c8fecd4a5455fc98ae94c to your computer and use it in GitHub Desktop.
Save rafapereirabr/c2299b650f0c8fecd4a5455fc98ae94c to your computer and use it in GitHub Desktop.
Create quantile by groups using data.table

quick code snippet to create (weighted) quantiles in R using data.table.

Create reproducible example

library(data.table)
library(Hmisc)

# create data.table
n <- 100000
dt <- data.table(pop=runif(n), income=runif(n))
dt$group <- c(rep('A', n/2), rep('B', n/2))
dt$year <- sample(2000:2005, n, replace = T)

simple quantile without weights

dt[, decile := cut(x = income, 
                   breaks = quantile(pop, probs = 0:10/10),
                   labels = FALSE, include.lowest = TRUE), 
          by = .(year, group) ]

quantile with weighted values

dt[, decile_w := cut(x = income, 
                   breaks = Hmisc::wtd.quantile(x = income, weights=pop, probs=0:10/10, 
                                                type=c('quantile','(i-1)/(n-1)','i/(n+1)','i/n'), 
                                                normwt=FALSE, na.rm=T),
                   labels = FALSE, include.lowest = TRUE), 
   by = .(year, group) ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment