Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate statistics (e.g. average) across cells of identical data-frames

Tags:

dataframe

r

I am having a list of identically sorted dataframes. More specific these are the imputed dataframes which I get after doing Multiple imputations with the AmeliaII package. Now I want to create a new dataframe that is identical in structure, but contains the mean values of the cells calculated across the dataframes.

The way I achieve this at the moment is the following:

## 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) 

I suppose that there is a much better way of doing this by using apply, plyr or the like, but as a R Newbie I am really a bit lost here. Do you have any suggestions how to go about this?

like image 520
Tungurahua Avatar asked Aug 12 '11 06:08

Tungurahua


2 Answers

Here's an alternate approach using Reduce and 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)

EDIT. You can simplify your code considerably and accomplish what you want in 5 lines of R code. Here is an example using the Amelia package.

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) 
like image 85
Ramnath Avatar answered Oct 12 '22 09:10

Ramnath


If I understand your question correctly, then this should get you a long way:

#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.

like image 23
Nick Sabbe Avatar answered Oct 12 '22 09:10

Nick Sabbe