计算相同数据帧的单元格的统计数据(例如平均值)

Tun*_*hua 5 r dataframe

我有一个相同排序的数据帧列表.更具体的是这些是在使用AmeliaII包进行多次插补后得到的估算数据帧.现在我想创建一个结构相同的新数据帧,但包含在数据帧中计算的单元格的平均值.

我目前实现这一目标的方式如下:

## do the Amelia run ------------------------------------------------------------

a.out <- amelia(merged, m=5, ts="Year", cs ="GEO",polytime=1)

## Calculate the output statistics ----------------------------------------------
left.side <- a.out$imputations[[1]][,1:2]
a.out.ncol <- ncol(a.out$imputations[[1]])

a <- a.out$imputations[[1]][,3:a.out.ncol]
b <- a.out$imputations[[2]][,3:a.out.ncol]
c <- a.out$imputations[[3]][,3:a.out.ncol]
d <- a.out$imputations[[4]][,3:a.out.ncol]
e <- a.out$imputations[[5]][,3:a.out.ncol]

# Calculate the Mean of the matrices
mean.right <- apply(abind(a,b,c,d,e,f,g,h,i,j,along=3),c(1,2),mean) 

# recombine factors with values
mean <- cbind(left.side,mean.right) 
Run Code Online (Sandbox Code Playgroud)

我想通过使用apply,plyr等有更好的方法来做到这一点,但作为一个R新手,我真的有点迷失在这里.你对此有什么建议吗?

Ram*_*ath 4

Reduce这是使用和的替代方法plyr::llply

dfr1 <- data.frame(a = c(1,2.5,3), b = c(9.0,9,9), c = letters[1:3])
dfr2 <- data.frame(a = c(5,2,5), b = c(6,5,4), c = letters[1:3])

tst = list(dfr1, dfr2)

require(plyr)
tst2 = llply(tst, function(df) df[,sapply(df, is.numeric)]) # strip out non-numeric cols
ans  = Reduce("+", tst2)/length(tst2)
Run Code Online (Sandbox Code Playgroud)

编辑。您可以大大简化代码并用 5 行 R 代码完成您想要的事情。这是使用 Amelia 包的示例。

library(Amelia)
data(africa)

# carry out imputations
a.out      = amelia(x = africa, cs = "country", ts = "year", logs = "gdp_pc") 

# extract numeric columns from each element of a.out$impuations  
tst2       = llply(a.out$imputations, function(df) df[,sapply(df, is.numeric)]) 

# sum them up and divide by length to get mean
mean.right = Reduce("+", tst2)/length(tst2)

# compute fixed columns and cbind with mean.right
left.side  = a.out$imputations[[1]][1:2]
mean0      = cbind(left.side,mean.right) 
Run Code Online (Sandbox Code Playgroud)