2011-08-12 18 views
5

की कोशिकाओं में आंकड़ों की गणना करें (उदाहरण के लिए औसत) मेरे पास समान क्रमबद्ध डेटाफ्रेम की एक सूची है। अधिक विशिष्ट ये अपर्याप्त डेटाफ्रेम हैं जो मुझे अमेलियाआई पैकेज के साथ एकाधिक अपूर्णताओं के बाद मिलता है। अब मैं एक नया डेटाफ्रेम बनाना चाहता हूं जो संरचना में समान है, लेकिन डेटाफ्रेम में गणना की गई कोशिकाओं के औसत मान शामिल हैं।समान डेटा-फ्रेम

तरह से मैं इस समय इस लक्ष्य को हासिल है निम्नलिखित:

## 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 या की तरह का उपयोग कर, द्वारा ऐसा करने का एक बेहतर तरीका है कि वहाँ है, लेकिन एक आर नौसिखिया मैं के रूप में वास्तव में यहाँ थोड़ा सा खो गया हूँ। क्या आपके पास कोई सुझाव है कि इस बारे में कैसे जाना है?

उत्तर

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) 

संपादित एक वैकल्पिक तरीका है। आप अपने कोड को सरल बना सकते हैं और आर कोड के 5 लाइनों में जो चाहते हैं उसे पूरा कर सकते हैं। अमेलिया पैकेज का उपयोग करके यहां एक उदाहरण दिया गया है।

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

धन्यवाद, वास्तव में मुझे एक लंबा सफर तय करता है। हालांकि, आपके समाधान के विपरीत, मेरे डेटाफ्रेम न केवल संख्यात्मक हैं बल्कि दो "कारक" कॉलम हैं जिन्हें मुझे सरणी का उपयोग करने से पहले "पट्टी" करने की आवश्यकता होगी। यदि आपको एक समाधान पता था जो "मिश्रित" डेटाफ्रेम पर भी काम करता है, तो यह मुझे "सभी तरह" प्राप्त करेगा। लेकिन जैसा कि पहले कहा गया था, आपका समाधान निश्चित रूप से पहले की तुलना में अधिक संक्षिप्त है। – 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,])) 
    }} 
संबंधित मुद्दे