data.frames列表中元素的平均值 [英] Mean of elements in a list of data.frames

查看:1545
本文介绍了data.frames列表中元素的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个data.frames列表(行和列相等)

  dat1 = as.data.frame (matrix(rnorm(25),ncol = 5))
dat2 = as.data.frame(matrix(rnorm(25),ncol = 5))
dat3 = as.data.frame (rnorm(25),ncol = 5))

all.dat< -list(dat1 = dat1,dat2 = dat2,dat3 = dat3)
/ pre>

如何返回单个data.frame,它是整个列表中data.frames中每个元素的平均值(或sum等)第一行和第一列的清单1,2,3等等)?我已经在plyr中尝试了lapply和ldply,但是这些返回列表中每个data.frame的统计信息。



编辑:由于某些原因,这被作为家庭作业重新插入。不是这么重要,但这不是一个功课问题,我只是不知道为什么我可以让这个工作。感谢任何见解!



编辑2:进一步澄清
我可以使用循环得到结果,但我希望有一种方法(更简单和更快方式是因为我使用的数据具有12行×100列的data.frame,并且存在这些数据帧中的1000+的列表。)

  z< -matrix(0,nrow(all.dat $ dat1),ncol(all.dat $ dat1))

for(l in 1:nrow(all.dat $ dat1)){
for(m in 1:ncol(all.dat $ dat1)){
z [l,m]< -mean(unlist(lapply(all.dat,`[ ,i = 1,j = m)))
}
}

其结果是:

 > z 
[,1] [,2] [,3] [,4] [,5]
[1,] -0.64185488 0.06220447 -0.02153806 0.83567173 0.3978507
[2,] -0.27953054 -0.19567085 0.45718399 -0.02823715 0.4932950
[3,] 0.40506666 0.95157856 1.00017954 0.57434125 -0.5969884
[4,] 0.71972821 -0.29190645 0.16257478 -0.08897047 0.9703909
[5,] -0.05570302 0.62045662 0.93427522 -0.55295824 0.7064439

我想知道是否有更少的笨重和更快的方法来做到这一点。谢谢!

解决方案

这是一个带 plyr 的班轮。你可以用你想要的任何其他功能替换 mean

  ans1 = aaply(laply(all.dat,as.matrix),c(2,3),mean)


Suppose I had a list of data.frames (of equal rows and columns)

dat1=as.data.frame(matrix(rnorm(25),ncol=5))
dat2=as.data.frame(matrix(rnorm(25),ncol=5))
dat3=as.data.frame(matrix(rnorm(25),ncol=5))

all.dat<-list(dat1=dat1,dat2=dat2,dat3=dat3)

How can I return a single data.frame that is the mean (or sum, etc) for each element in the data.frames across the list (e.g., mean of first row and first column from lists 1, 2, 3 and so on)? I have tried lapply and ldply in plyr but these return the statistic for each data.frame within the list.

Edit: For some reason, this was retagged as homework. Not that it matters either way, but this is not a homework question I just don't know why I can get this to work. Thanks for any insight!

Edit2: For further clarification I can get the results using loops, but I was hoping that there was a way (simpler and faster way because the data I am using has data.frames that are 12 rows by 100 columns and there is a list of 1000+ of these data frames.)

z<-matrix(0,nrow(all.dat$dat1),ncol(all.dat$dat1))

for(l in 1:nrow(all.dat$dat1)){
   for(m in 1:ncol(all.dat$dat1)){
      z[l,m]<-mean(unlist(lapply(all.dat, `[`, i =l, j = m)))
   }
}

with a result of the means:

> z
        [,1]        [,2]        [,3]        [,4]       [,5]
[1,] -0.64185488  0.06220447 -0.02153806  0.83567173  0.3978507
[2,] -0.27953054 -0.19567085  0.45718399 -0.02823715  0.4932950
[3,]  0.40506666  0.95157856  1.00017954  0.57434125 -0.5969884
[4,]  0.71972821 -0.29190645  0.16257478 -0.08897047  0.9703909
[5,] -0.05570302  0.62045662  0.93427522 -0.55295824  0.7064439

I was wondering if there was a less clunky and faster way to do this. Thanks!

解决方案

Here is a one liner with plyr. You can replace mean with any other function that you want.

ans1 = aaply(laply(all.dat, as.matrix), c(2, 3), mean)

这篇关于data.frames列表中元素的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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