简体   繁体   English

如果表具有不同的长度,如何将几个表中的列的值相加?

[英]How do I sum the values of columns in several tables if tables have different lengths?

Alright, this should be an easy one but I'm looking for a solution that's as fast as possible. 好吧,这应该是一个简单的,但我正在寻找一个尽可能快的解决方案。

Let's say I have 3 tables (the number of tables will be much larger): 假设我有3个表(表的数量会大得多):

tab1 <- table(c(1, 1, 1, 2, 2, 3, 3, 3))
tab2 <- table(c(1, 1, 4, 4, 4))
tab3 <- table(c(1, 1, 2, 3, 5))

This is what we get: 这就是我们得到的:

> tab1
1 2 3 
3 2 3 
> tab2
1 4 
2 3 
> tab3
1 2 3 5 
2 1 1 1 

What I want to have in a fast way so that it works with many big tables is this: 我希望以快速的方式拥有它以便它适用于许多大表是:

1 2 3 4 5
7 3 4 3 1

So, basically the tables get aggregated over all names . 因此,基本上表格会聚合在所有names Is there an elementary function that does this which I am missing? 是否有基本功能可以解决这个问题? Thanks for your help! 谢谢你的帮助!

We concatenate ( c ) the tab output to create 'v1', use tapply to get the sum of the elements grouped by the names of that object. 我们连接( ctab输出以创建'v1',使用tapply来获取按该对象的names分组的元素的sum

v1 <- c(tab1, tab2, tab3)
tapply(v1, names(v1), FUN=sum)
#1 2 3 4 5 
#7 3 4 3 1 

You could use rowsum() . 你可以使用rowsum() The output will be slightly different than what you show, but you can always restructure it after the calculations. 输出与您显示的输出略有不同,但您可以在计算后重新进行重组。 rowsum() is known to be very efficient. 已知rowsum()非常有效。

x <- c(tab1, tab2, tab3)
rowsum(x, names(x))
#   [,1]
# 1    7
# 2    3
# 3    4
# 4    3
# 5    1

Here's a benchmark with akrun's data.table suggestion added in as well. 这里是akrun的data.table建议的基准。

library(microbenchmark)
library(data.table)

xx <- rep(x, 1e5)

microbenchmark(
    tapply = tapply(xx, names(xx), FUN=sum),
    rowsum = rowsum(xx, names(xx)),
    data.table = data.table(xx, names(xx))[, sum(xx), by = V2]
)
# Unit: milliseconds
#        expr       min        lq      mean    median        uq       max neval
#      tapply 150.47532 154.80200 176.22410 159.02577 204.22043 233.34346   100
#      rowsum  41.28635  41.65162  51.85777  43.33885  45.43370 109.91777   100
#  data.table  21.39438  24.73580  35.53500  27.56778  31.93182  92.74386   100

you can try this 你可以试试这个

df <- rbind(as.matrix(tab1), as.matrix(tab2), as.matrix(tab3))
aggregate(df, by=list(row.names(df)), FUN=sum)
  Group.1 V1
1       1  7
2       2  3
3       3  4
4       4  3
5       5  1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM