对特定列的 data.table 行求和 [英] Summing across rows of a data.table for specific columns

查看:17
本文介绍了对特定列的 data.table 行求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大数据表(来自包 data.表)有60多列(前三列对应因子,其余对应响应变量,在这种情况下是不同的物种)和几行对应于不同的处理水平和物种丰度.一个非常小的版本如下所示:

I have a large data table (from the package data.table) with over 60 columns (the first three corresponding to factors and the remaining to response variables, in this case different species) and several rows corresponding to the different levels of the treatments and the species abundances. A very small version looks like this:

library(data.table)
TEST <- data.table(Time=c("0","0","0","7","7","7","12"),
             Zone=c("1","1","0","1","0","0","1"),
             quadrat=c(1,2,3,1,2,3,1),
             Sp1=c(0,4,29,9,1,2,10),
             Sp2=c(20,17,11,15,32,15,10),
             Sp3=c(1,0,1,1,1,1,0))

setkey(TEST,Time)
TEST

#    Time Zone quadrat Sp1 Sp2 Sp3
# 1:    0    1       1   0  20   1
# 2:    0    1       2   4  17   0
# 3:    0    0       3  29  11   1
# 4:   12    1       1  10  10   0
# 5:    7    1       1   9  15   1
# 6:    7    0       2   1  32   1
# 7:    7    0       3   2  15   1

我首先想计算每个区域 x 样方组合在时间上每个物种的平均丰度,这很好:

I first want to calculate the mean abundances of each species across Time for each Zone x quadrat combination and that's fine:

Abundance = TEST[ , lapply(.SD, mean), by = "Zone,quadrat"]
Abundance
#    Zone quadrat Time       Sp1  Sp2       Sp3
# 1:   Z1       1   NA  6.333333 15.0 0.6666667
# 2:   Z1       2   NA  2.500000 24.5 0.5000000
# 3:   Z0       1   NA 15.500000 13.0 1.0000000  

然后我想计算物种"列的逐行总和,在从 Sp1 到 Sp3 的示例中.我尝试了以下代码但没有成功:

Then I want to calculate rowwise sum for the 'species' columns, in the example from Sp1 to Sp3. I have tried the following code with no success:

Abundance$SumAbundance <- rowSums(Abundance[ , c(4:6)])  

我收到错误消息:

# Error in rowSums(Abundance[, c(4:6)]) : 
# 'x' must be an array of at least two dimensions

如何计算 data.table 的特定列的行总和?

How can I compute row sums for specific columns of a data.table?

推荐答案

[ 2020-02-15 编辑以反映 data.table 的当前状态] 最近的版本data.table rowSums(Abundance[ , 4:6]) 按照 OP 最初的预期工作.以下是一些替代方案:

[ Edited 2020-02-15 to reflect current state of data.table ] In recent versions of data.table rowSums(Abundance[ , 4:6]) works as OP originally expected. Here are some alternatives:

Abundance[, SumAbundance := rowSums(.SD), .SDcols = 4:6]

另外,我没有检查,但我怀疑这会更快,因为它不会像 rowSums 那样转换为 matrix:

Also, I didn't check, but I have a suspicion this will be faster, since it will not convert to matrix as rowSums does:

Abundance[, SumAbundance := Reduce(`+`, .SD), .SDcol = 4:6]

这篇关于对特定列的 data.table 行求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆