Created
April 22, 2013 11:38
-
-
Save oscarperpinan/5434094 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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