Tutorial by Examples

# example data DT = data.table(iris) DT[, Bin := cut(Sepal.Length, c(4,6,8))] Using .N .N in j stores the number of rows in a subset. When exploring data, .N is handy to... count rows in a group, DT[Species == "setosa", .N] # 50 or count rows in all groups, DT[, .N,...
# example data DT = data.table(iris) DT[, Bin := cut(Sepal.Length, c(4,6,8))] Suppose we want the summary function output for Sepal.Length along with the number of observations: DT[, c( as.list(summary(Sepal.Length)), N = .N ), by=.(Species, Bin)] # Species Bin Min. 1st Q...
# example data DT = data.table(iris) DT[, Bin := cut(Sepal.Length, c(4,6,8))] summary is handy for browsing summary statistics. Besides direct usage like summary(DT), it can also be applied per-group conveniently with split: lapply(split(DT, by=c("Species", "Bin"), drop=TRU...
# example data DT = data.table(iris) DT[, Bin := cut(Sepal.Length, c(4,6,8))] To apply the same summarizing function to every column by group, we can use lapply and .SD DT[, lapply(.SD, median), by=.(Species, Bin)] # Species Bin Sepal.Length Sepal.Width Petal.Length Petal.Width # 1...

Page 1 of 1