2011-08-12 3 views
5

同じソートデータフレームのリストがあります。より具体的には、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) 

私は、適用plyr等を用いてこれを行うには、もっと良い方法があると仮定しますが、R初心者Iとして本当にここで少し失われています。あなたはこれについてどうやって行くのですか?

答えて

4

ここReduceplyr::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) 

EDITを使用して別のアプローチがあります。コードを大幅に簡素化し、5行のRコードで必要なものを達成することができます。ここに、Ameliaパッケージを使用した例があります。

4

私が正しくあなたの質問を理解していれば、これはあなたに長い道のりを取得する必要があります:

#set up some data: 
dfr1<-data.frame(a=c(1,2.5,3), b=c(9.0,9,9)) 
dfr2<-data.frame(a=c(5,2,5), b=c(6,5,4)) 
tst<-list(dfr1, dfr2) 
#since all variables are numerical, use a threedimensional array 
tst2<-array(do.call(c, lapply(tst, unlist)), dim=c(nrow(tst[[1]]), ncol(tst[[1]]), length(tst))) 
#To see where you're at: 
tst2 
#rowMeans for a threedimensional array and dims=2 does the mean over the last dimension 
result<-data.frame(rowMeans(tst2, dims=2)) 
rownames(result)<-rownames(tst[[1]]) 
colnames(result)<-colnames(tst[[1]]) 
#display the full result 
result 

HTHを。

+0

ありがとうございます、実際には私には長い道のりがあります。しかし、あなたのソリューションとは異なり、私のデータフレームは数値だけではなく、配列を使用する前に "ストリップ"する必要がある2つの "factor"カラムを持っています。 「混在した」データフレームでも動作するソリューションを知っていれば、それは私を「すべて」得ることになります。しかし、前にも述べたように、あなたのソリューションは、私が以前使用していたソリューションよりもはるかに簡潔です。 – Tungurahua

+0

私が正しく覚えていれば、私が提供したリストの解決策はまだほとんど機能します。係数は数値に強制され、その平均が取られます(ほとんど無意味なので安全に無視することができます)。 –

1

多くの試みの後、私は、複数のデータフレームにわたってセルの平均を計算する合理的に速い方法を見出しました。

# First create an empty data frame for storing the average imputed values. This 
# data frame will have the same dimensions of the original one 

imp.df <- df 

# Then create an array with the first two dimensions of the original data frame and 
# the third dimension given by the number of imputations 

a <- array(NA, dim=c(nrow(imp.df), ncol(imp.df), length(a.out$imputations))) 

# Then copy each imputation in each "slice" of the array 

for (z in 1:length(a.out$imputations)) { 
a[,,z] <- as.matrix(a.out$imputations[[z]]) 
} 

# Finally, for each cell, replace the actual value with the mean across all 
# "slices" in the array 

for (i in 1:dim(a)[1]) { 
    for (j in 1:dim(a)[2]) { 
imp.df[i, j] <- mean(as.numeric(a[i, j,])) 
    }} 
関連する問題