DT[ i, j, by ]

data.table下
DT[ i, j, by ] # + extra arguments
| | |
| | -------> grouped by what?
| -------> what to do?
---> on which rows?

X[, a]    # return col 'a' from X as vector. If not found, search in parent frame.

X[, .(a)] # same as above, but return as a data.table.

X[, sum(a)] # return sum(a) as a vector (with same scoping rules as above)

X[, .(sum(a)), by=c] # get sum(a) grouped by 'c'.

X[, sum(a), by=c] # same as above, .() can be ommitt
ed in by on single expression for convenience

X[, sum(a), by=c:f] # get sum(a) grouped by all columns in between 'c' and 'f' (both inclusive)

X[, sum(a), keyby=b] # get sum(a) grouped by 'b', and sort that result by the grouping column 'b'

X[, sum(a), by=b][order(b)] # same order as above, but by chaining compound expressions

X[c>1, sum(a), by=c] # get rows where c>1 is TRUE, and on those rows, get sum(a) grouped by 'c'

X[Y, .(a, b), on="c"] # get rows where Y$c == X$c, and select columns 'X$a' and 'X$b' for those rows

X[Y, .(a, i.a), on="c"] # get rows where Y$c == X$c, and then select 'X$a' and 'Y$a' (=i.a)

X[Y, sum(a*i.a), on="c" by=.EACHI] # for *each* 'Y$c', get sum(a*i.a) on matching rows in 'X$c'

X[, plot(a, b), by=c] # j accepts any expression, generates plot for each group and returns no data

# see ?assign to add/update/delete columns by reference using the same consistent interface
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Lua 5.1 参考手册 by Roberto Ierusalimschy, Luiz Henrique de F...
    苏黎九歌阅读 13,931评论 0 38
  • 我成为销售精英每一天, 昨天听了老师的授课,一个小时左右的课程,虽然没完全消化,却让我感触很深。一直以来,我从来没...
    迷糊Y头阅读 113评论 0 1
  • 画面是自我问题的表象呈现,只解决如何画没用。 专注力,自我约束能力,发散思维,逻辑思维,肌肉发育程度,抽象具象思维...
    Pan潘阅读 326评论 2 1
  • 阅读时间2017年6月1日 阅读p7~12页 问:他们本来是去找吃的,为什么他们又去寻找妈妈了呢? 答:因为找不到...
    陆_79f8阅读 235评论 0 1
  • 一切简单却并不是真的简单、也许这就是我所理解的奢华。 繁华落尽已成真、回归自然与本真,但、有生之年要去经历,否则你...
    素朴一陶阅读 119评论 0 0