问题描述
我想使用尝试执行简单的总和和平均值,但我得到意想不到的结果。我遵循 for data.table。我发现一种方式工作,但我不知道为什么这个方法在FAQ的第2节不是。
这个方法给我不正确的结果(例如,它给我第一列的值):
基因TCGA_04_1348 TCGA_04_1362 genesum
1:A1BG 0.94565 0.70585 0.94565
2:A1BG-AS 0.97610 1.15850 0.97610
3:A1CF 0.00000 0.02105 0.00000
4:A2BP1 0.00300 0.04150 0.00300
5:A2LD1 4.57975 5.02820 4.57975
6:A2M 60.37320 36.09715 60.37320
这给了我想要的结果。
基因TCGA_04_1348 TCGA_04_1362 genesum
1:A1BG 0.94565 0.70585 1.65150
2:A1BG-AS 0.97610 1.15850 2.13460
3:A1CF 0.00000 0.02105 0.02105
4:A2BP1 0.00300 0.04150 0.04450
5:A2LD1 4.57975 5.02820 9.60795
6:A2M 60.37320 36.09715 96.47035
我有更多的列和行,这只是一个子集。这与我设置键的方式有什么关系吗?
gene TCGA_04_1348 TCGA_04_1362 genesum
1: A1BG 0.94565 0.70585 0.94565
2: A1BG-AS 0.97610 1.15850 0.97610
3: A1CF 0.00000 0.02105 0.00000
4: A2BP1 0.00300 0.04150 0.00300
5: A2LD1 4.57975 5.02820 4.57975
6: A2M 60.37320 36.09715 60.37320
and this is giving me the desired result
gene TCGA_04_1348 TCGA_04_1362 genesum
1: A1BG 0.94565 0.70585 1.65150
2: A1BG-AS 0.97610 1.15850 2.13460
3: A1CF 0.00000 0.02105 0.02105
4: A2BP1 0.00300 0.04150 0.04450
5: A2LD1 4.57975 5.02820 9.60795
6: A2M 60.37320 36.09715 96.47035
I have many more columns and rows, this is just a subset. Does this have anything to do with the way I set the key?
NAME NROW MB COLS KEY
[1,] dt 20,785 2 gene,TCGA_04_1348_01A,TCGA_04_1362_01A,genesum gene
A few things:
dt[, genesum:=lapply(.SD,sum), by=gene]
anddt[, genesum:=apply(dt[,-1, with=FALSE],1, sum)]
are quite different.dt[, genesum:=lapply(.SD,sum), by=gene]
loops over the columns of the.SD
data.table and sums themdt[, genesum:=apply(dt[,-1, with=FALSE],1, sum)]
is looping over the rows (ie.apply(x, 1, function)
appliesfunction
to every row inx
I think you can get what you want by calling
rowSums
, like so:dt[, genesum := rowSums(dt[, -1, with=FALSE])]
Is that what you're after?
这篇关于data.table中的行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!