Skip to content

Instantly share code, notes, and snippets.

@oscarperpinan
Created April 22, 2013 11:38
Show Gist options
  • Save oscarperpinan/5434094 to your computer and use it in GitHub Desktop.
Save oscarperpinan/5434094 to your computer and use it in GitHub Desktop.
library(data.table)
library(lattice)
## Create a toy example
dt <- data.table(A=rnorm(1e7, mean=0, sd=1))
## Bin the data using equal-width intervals
brks <- seq(-6, 6, length=10000)
## and := to avoid an additional copy
dt[,bin:=findInterval(A, brks)]
## Finally aggregate with mean by bin
## .N to include number of elements per bin
dt2 <- dt[, list(.N, m=mean(A)), by=bin]
xyplot(N ~ m, data=dt2, alpha=0.1, pch=21, fill='blue', col='black')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment