从数据帧列表中将选定的数据帧加在一起 [英] Adding selected data frames together, from a list of data frames
问题描述
当尝试将我的微观解决方案应用于宏观规模时,我遇到了一个大问题.我想编写一个函数,使我可以自动将特定数据帧的所有值加在一起.
I encountered big problem when trying to apply my micro solution to macro scale. I want to write a function that will allow me to automatize adding all values of specific data frames together.
首先,我创建了所有数据帧的列表:
First, I have created list of all data frames:
> lst
$data001
A B C D E
X 10 30 50 70
Y 20 40 60 80
$data002
A B C D E
X 10 30 50 70
Y 20 40 60 80
$data003
A B C D E
X 10 30 50 70
Y 20 40 60 80
Z 20 40 60 80
$data004
A B C D E
X 10 30 50 70
Y 20 40 60 80
Z 20 40 60 80
V 20 40 60 80
$data005
A B C D E
Q 10 30 50 70
$data006
A B C D E
X 10 30 50 70
Y 20 40 60 80
$data007
A B C D E
X 10 30 50 70
Y 20 40 60 80
$data008
A B C D E
X 10 30 50 70
Y 20 40 60 80
$data09
A B C D E
X 11 33 55 77
Y 22 44 66 88
$data010
A B C D E
X 10 30 50 70
Y 20 40 60 80
第二,我确定了我想将哪些数据帧加在一起(将1加到1和将2加到2等).在此示例中,在lst
内的以下顺序中有10个数据帧:
Second, I have determined which data frames I would like to add together (add 1 to 1 and 2 to 2 etc.). In this example there are 10 data frames organized in the following order, within lst
:
[1] 1 1 2 2 2 2 2 2 3 2
手动添加所有一个",我将看起来像这样:
Manually adding all "ones" I would look something like this:
> ddply(rbind(lst[[1]],lst[[2]]), "A", numcolwise(sum))
A B C D E
X 20 60 100 140
Y 40 80 120 160
手动添加所有两个",我将看起来像这样:
Manually adding all "two" I would look something like this:
A B C D E
X 60 180 300 420
Y 120 240 360 480
Z 40 80 120 160
V 20 40 60 80
Q 10 30 50 70
但是,我只是无法弄清楚如何编写一个循环来创建列表,在这个示例中,该列表将包含3个数据帧,这些数据帧是对选定数据帧求和的结果.
However, I just cannot figure it out how write a loop that will create list with, in this example, 3 data frames that are result of summing up selected data frames.
提前谢谢!
推荐答案
我们可能会使用data.table
library(data.table)
lapply(split(seq_along(lst), v1), function(i)
rbindlist(lst[i], fill=TRUE)[
, lapply(.SD, sum), A, .SDcols= B:E])
#$`1`
# A B C D E
#1: X 20 60 100 140
#2: Y 40 80 120 160
#$`2`
# A B C D E
#1: X 60 180 300 420
#2: Y 120 240 360 480
#3: Z 40 80 120 160
#4: V 20 40 60 80
#5: Q 10 30 50 70
#$`3`
# A B C D E
#1: X 11 33 55 77
#2: Y 22 44 66 88
数据
v1 <- c(1, 1, 2, 2, 2, 2, 2, 2, 3, 2)
这篇关于从数据帧列表中将选定的数据帧加在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!