使用plyr软件包和处理列表时遇到问题
我很难理解plyr软件包的用法。我尝试使用它来拆分存储在列表中的数据帧,应用函数,将结果存储为数据帧,并再次将数据帧组合为列表。 鉴于以下数据:使用plyr软件包和处理列表时遇到问题,r,plyr,R,Plyr,我很难理解plyr软件包的用法。我尝试使用它来拆分存储在列表中的数据帧,应用函数,将结果存储为数据帧,并再次将数据帧组合为列表。 鉴于以下数据: #create test dfs df1<-data.frame(a=sample(1:50,10),b=sample(1:50,10),c=sample(1:50,10),d=(c("a","b","c","a","a","b","b","a","c","d"))) df2<-data.frame(a=sample
#create test dfs
df1<-data.frame(a=sample(1:50,10),b=sample(1:50,10),c=sample(1:50,10),d=(c("a","b","c","a","a","b","b","a","c","d")))
df2<-data.frame(a=sample(1:50,9),b=sample(1:50,9),c=sample(1:50,9),d=(c("e","f","g","e","e","f","f","e","g")))
df3<-data.frame(a=sample(1:50,8),b=sample(1:50,8),c=sample(1:50,8),d=(c("h","i","j","h","h","i","i","h")))
#make them a list
list.1<-list(df1=df1,df2=df2,df3=df3)
但是如何将其应用于数据帧中的每一列以及列表中的每一个数据帧?我如何重新分配所有数据,以便最终得到一个包含结果的矩阵列表?很抱歉这个非常基本的问题,但我是R的新手,我已经很长时间试图解决这个问题了。。。thx.您需要将所有数据放在一个大的
数据框中。
:
library(reshape)
big_dataframe = ldply(list.1, function(x) melt(x, id.vars = "d"))
> head(big_dataframe)
.id d variable value
1 df1 a a 44
2 df1 b a 17
3 df1 c a 15
4 df1 a a 30
5 df1 a a 49
6 df1 b a 33
…然后在其上使用ddply
res = ddply(big_dataframe, .(.id, d, variable), summarise, mn = mean(value))
> res
.id d variable mn
1 df1 a a 40.00000
2 df1 a b 25.25000
3 df1 a c 31.25000
4 df1 b a 22.66667
5 df1 b b 16.00000
6 df1 b c 26.00000
7 df1 c a 9.00000
8 df1 c b 16.50000
9 df1 c c 15.00000
10 df1 d a 28.00000
11 df1 d b 24.00000
12 df1 d c 39.00000
13 df2 e a 18.50000
14 df2 e b 15.50000
15 df2 e c 16.50000
16 df2 f a 26.33333
17 df2 f b 42.00000
18 df2 f c 37.00000
19 df2 g a 26.50000
20 df2 g b 22.00000
21 df2 g c 31.00000
22 df3 h a 29.25000
23 df3 h b 34.25000
24 df3 h c 32.00000
25 df3 i a 30.33333
26 df3 i b 40.00000
27 df3 i c 24.33333
28 df3 j a 21.00000
29 df3 j b 5.00000
30 df3 j c 46.00000
它给出了每个变量(
a
-c
)的平均值,每个因子级别d
,以及每个子数据帧(df1-df3)。这里是一个结合llply()
和ddply()
的解决方案。首先,llply()。然后对列表的每个数据帧应用ddply()
,并根据列d
划分每个数据帧。函数colMeans()
用于计算每个数值列的平均值
llply(list.1,function(x) ddply(x,.(d),function(x) colMeans(x[,1:3])))
$df1
d a b c
1 a 22.25000 26.25 34.25000
2 b 19.66667 22.00 28.66667
3 c 37.00000 44.50 18.00000
4 d 17.00000 3.00 4.00000
$df2
d a b c
1 e 20.50000 32.25000 18.5
2 f 25.33333 34.33333 21.0
3 g 20.50000 26.50000 16.5
$df3
d a b c
1 h 17.5 26.50000 37.25000
2 i 45.0 22.33333 26.33333
3 j 25.0 33.00000 42.00000
您可以始终使用lappy
您的ddply
:
lapply(list.1, function(x) ddply(x, .(d), function(x)
data.frame(a=mean(x$a),b=mean(x$b),c= mean(x$c))) )
或者完全使用您的代码:
lapply(list.1, function(x) ddply(x,.(d),summarise, mean=mean(a)) )
非常感谢。这就是我在想的,但我不知道如何命名ddply中的对象。现在,使用内部函数,这一切都是有意义的……我认为第一个代码在结尾处遗漏了一个)。它工作正常,但我必须手动索引所有列。downer代码只返回.perfect和易于使用的方法。这就是我所想的,但我不知道如何命名ddply中的对象。像这样,它现在可以工作了!非常感谢。
lapply(list.1, function(x) ddply(x,.(d),summarise, mean=mean(a)) )