美文网首页
DT[ i, j, by ]

DT[ i, j, by ]

作者: 茶苯海 | 来源:发表于2018-01-23 16:15 被阅读8次

    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
    

    相关文章

      网友评论

          本文标题:DT[ i, j, by ]

          本文链接:https://www.haomeiwen.com/subject/grwsaxtx.html