使用data.table进行聚合

day*_*yne 5 aggregate r data.table

在SO用户的多个建议之后,我终于尝试将我的代码转换为使用data.table.

library(data.table)
DT <- data.table(plate = paste0("plate",rep(1:2,each=5)),
             id = rep(c("CTRL","CTRL","ID1","ID2","ID3"),2),
             val = 1:10)

> DT
    plate   id val
1: plate1 CTRL   1
2: plate1 CTRL   2
3: plate1  ID1   3
4: plate1  ID2   4
5: plate1  ID3   5
6: plate2 CTRL   6
7: plate2 CTRL   7
8: plate2  ID1   8
9: plate2  ID2   9
10: plate2  ID3  10
Run Code Online (Sandbox Code Playgroud)

我想做的是DT[,val]当id为"CTRL"时取平均值.

我通常会aggregate使用数据框,然后使用match将值映射回新列'ctrl'.

使用data.table包我可以得到:

DT[id=="CTRL",ctrl:=mean(val),by=plate]

> DT
    plate   id val ctrl
1: plate1 CTRL   1  1.5
2: plate1 CTRL   2  1.5
3: plate1  ID1   3   NA
4: plate1  ID2   4   NA
5: plate1  ID3   5   NA
6: plate2 CTRL   6  6.5
7: plate2 CTRL   7  6.5
8: plate2  ID1   8   NA
9: plate2  ID2   9   NA
10: plate2  ID3  10   NA
Run Code Online (Sandbox Code Playgroud)

我真正需要的是:

DT <- data.table(plate = paste0("plate",rep(1:2,each=5)),
                 id = rep(c("CTRL","CTRL","ID1","ID2","ID3"),2),
                 val = 1:10,
                 ctrl = rep(c(1.5,6.5),each=5))

> DT
    plate   id val ctrl
1: plate1 CTRL   1  1.5
2: plate1 CTRL   2  1.5
3: plate1  ID1   3  1.5
4: plate1  ID2   4  1.5
5: plate1  ID3   5  1.5
6: plate2 CTRL   6  6.5
7: plate2 CTRL   7  6.5
8: plate2  ID1   8  6.5
9: plate2  ID2   9  6.5
10: plate2  ID3  10  6.5
Run Code Online (Sandbox Code Playgroud)

最后我想使用更复杂的值选择,但我不知道如何选择特定值,运行一些函数,然后使用数据框将这些值映射回适当的行.

Fra*_*ank 7

这是你想要做的:

DT[,ctrl:=mean(val[id=="CTRL"]),by=plate]
Run Code Online (Sandbox Code Playgroud)

这使

     plate   id val ctrl
 1: plate1 CTRL   1  1.5
 2: plate1 CTRL   2  1.5
 3: plate1  ID1   3  1.5
 4: plate1  ID2   4  1.5
 5: plate1  ID3   5  1.5
 6: plate2 CTRL   6  6.5
 7: plate2 CTRL   7  6.5
 8: plate2  ID1   8  6.5
 9: plate2  ID2   9  6.5
10: plate2  ID3  10  6.5
Run Code Online (Sandbox Code Playgroud)

你的原始代码DT[id=="CTRL",ctrl:=mean(val),by=plate]没有对id=="CTRL"不正确的行进行赋值,因为当你使用第一个参数时[,你是子集; 第二个参数中的操作仅针对子集化进行data.table.