Fast melted data.table operations

前端 未结 3 1492
孤城傲影
孤城傲影 2021-01-05 04:50

I am looking for patterns for manipulating data.table objects whose structure resembles that of dataframes created with melt from the reshape

3条回答
  •  余生分开走
    2021-01-05 05:36

    > setkey(input, "id")
    > input[ , list(sum(value)), by=id]
       id V1
    1:  1  6
    2:  2 15
    3:  3 34
    
    > input[ variable %in% c("x", "y"), list(sum(value)), by=id]
       id V1
    1:  1  6
    2:  2 15
    3:  3 24
    

    The last one:

    > input[ variable %in% c("x", "y"), list(sum(value)), by=list(id, variable)]
       id variable V1
    1:  1        x  1
    2:  1        y  5
    3:  2        x  4
    4:  2        y 11
    5:  3        x 15
    6:  3        y  9
    

提交回复
热议问题