Using data.table for binning

[This article was first published on Omnia sunt Communia! » R-english, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I discovered the impressive data.table package more than a year ago. In order to learn how to use it, I try to find a solution to some questions I read at mailing lists or at stackoverflow. My last experiment has been inspired by the bigvis package and its associated paper. This package is a proposal for exploratory data analysis of large datasets following the workflow of binning, summarizing and display.
Please note that I am neither trying to mimic the behavior of bigvis nor comparing both packages. It is only an excuse to learn more about data.table and this post shows the code I have used.
The first part of my experiment deals with one-dimensional data:

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')
view raw DT_Bin1D.R hosted with ❤ by GitHub

dt1d
The second part is more sophisticated. It uses the movie dataset to show how to carry out 2D binning.

library(data.table)
data(movies, package='ggplot2')
library(lattice)
movies <- data.table(movies)
nBins <- 500
## 2D Binning
movies[, c('ratBin', 'votesBin'):=list(floor(rating/nBins), floor(votes/nBins))]
setkey(movies, ratBin, votesBin)
## Cross Join (as expand.grid) ...
bins <- CJ(0:nBins, 0:nBins)
## ... used to extract records from movies corresponding to each 2D bin
movies2 <- movies[bins, list(.N, rating, votes), nomatch=0]
## Finally aggregate with mean
movies3 <- movies2[,lapply(.SD, mean),
by=list(ratBin, votesBin)]
xyplot(votes ~ rating, data=movies3, cex=asinh(movies3$N)/5,
alpha=0.6, pch=21, fill='blue', col='black')
view raw DT_Bin2D.R hosted with ❤ by GitHub

dt2d

Some key points I have learned about data.table:

  • := to add, remove or modify by reference (avoids memory overhead since it does not make additional copies)
  • .N and .SD symbols for grouping

Still learning!


To leave a comment for the author, please follow the link and comment on their blog: Omnia sunt Communia! » R-english.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)