汇总data.table中特定列的行 [英] Summing across rows of a data.table for specific columns

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

问题描述

我有一个很大的数据表(来自软件包数据。表格)(超过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

推荐答案

[已编辑2020-02-15,以反映数据的当前状态.table ]在最新版本的 data.table rowSums(Abundance [,4:6]) 可以像最初预期的那样工作。以下是一些替代方案:

[ 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天全站免登陆