[R] Mean of values in different files

arun smartpink111 at yahoo.com
Fri Apr 11 10:53:20 CEST 2014

lst1 <- split(list.files(pattern=".csv"),gsub("\\_.*","",list.files(pattern=".csv"))) lst2 <-  lapply(lst1,function(x1) lapply(x1, function(x2) {lines1 <- readLines(x2); header1 <- lines1[1:2]; dat1 <- read.table(text=lines1,header=FALSE,sep=",",stringsAsFactors=FALSE, skip=2); colnames(dat1) <- Reduce(paste,strsplit(header1,","));dat1}))

lstYear <- lapply(lst2,function(x) lapply(x, function(y) y[,1,drop=FALSE])[[1]]) 

lapply(seq_along(lst2),function(i) {lstN <-lapply(lst2[[i]],function(x) x[,-1]); arr1 <- array(unlist(lstN),dim=c(dim(lstN[[1]]),length(lstN)),dimnames=list(NULL,lapply(lstN,names)[[1]]));res <- cbind(lstYear[[i]],rowMeans(arr1,dims=2,na.rm=TRUE)); names(res) <- gsub("\\_$","",gsub(" ", "_",names(res))); res[,1] <- gsub(" <", "",res[,1]); write.csv(res,paste0(paste(getwd(),"final",names(lst1)[[i]],sep="/"),".csv"),row.names=FALSE,quote=FALSE)   }) 


On Thursday, April 10, 2014 11:55 PM, Zilefac Elvis <zilefacelvis at yahoo.com> wrote:

I have finished calculating my statistics and would like to merge the files into 1 files resulting from 100 simulations.

In the attached .zip file, I have 4 files for site G100_ and 4 for site G101_ identified by site code. I have 12000 (120 sites * 100 sims) such files to deal with but I have provided a sample only.

1) Replace any empty spaces (no values) with NA
2) Take the mean of values in each cell

Create a folder called "final" and store the results in it. For this sample, the folder "final" will have two files (G100 and G101). The contents of each file being the mean from 4 files. The headers, rows and columns of files in "final" are exactly the same as those of any file in sample.zip. Headers, rows and columns are the same for all files in sample.zip.

Thanks AK. Please do it at your convenience.

More information about the R-help mailing list