### Q1: working directory: Observed #Only one file per Site. Assuming this is the ### case for the full dataset, then I guess there is no need to average dir.create("final") 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[-c(nrow(dat1), nrow(dat1) - 1), ] })) lst3 <- lst2[sapply(seq_along(lst2),function(i){lstN <- sapply(lst2[[i]],function(x) is.integer(ncol(x)))})] length(lst2) #[1] 120 length(lst3) #[1] 119 library(plyr) library(stringr) lst4 <- setNames(lapply(seq_along(lst3), function(i) { lapply(lst3[[i]], function(x) { names(x)[-1] <- paste(names(x)[-1], names(lst1)[i], sep = "_") names(x) <- str_trim(names(x)) x })[[1]] }), names(lst3)) df1 <- join_all(lst4, by = "Year") dim(df1) # [1] 9 27311 dimCol <- sapply(split(names(df1)[-1], gsub(".*\\_", "", names(df1)[-1])), function(x) { df2 <- df1[, x] df3 <- data.frame(Percentiles = paste0(seq(0, 100, by = 1), "%"), numcolwise(function(y) quantile(y, seq(0, 1, by = 0.01), na.rm = TRUE))(df2), stringsAsFactors = FALSE) ncol(df3) }) lst5 <- split(names(df1)[-1], gsub(".*\\_", "", names(df1)[-1])) lapply(seq_along(lst5), function(i) { df2 <- df1[, lst5[[i]]] df3 <- data.frame(Percentiles = paste0(seq(0, 100, by = 1), "%"), numcolwise(function(y) quantile(y, seq(0, 1, by = 0.01), na.rm = TRUE))(df2), stringsAsFactors = FALSE) write.csv(df3, paste0(paste(getwd(), "final", paste(names(lst4)[[i]], "Quantile", sep = "_"), sep = "/"), ".csv"), row.names = FALSE, quote = FALSE) }) ReadOut1 <- lapply(list.files(recursive = TRUE)[grep("Quantile", list.files(recursive = TRUE))], function(x) read.csv(x, header = TRUE, stringsAsFactors = FALSE)) dir.create("Indices") sapply(ReadOut1, dim)[,1:3] ##different dimensions # [,1] [,2] [,3] #[1,] 101 101 101 #[2,] 157 258 258 names1 <- unlist(lapply(ReadOut1, function(x) names(x)[-1])) names2 <- gsub("\\_.*", "", names1) names3 <- unique(gsub("[.]", " ", names2)) length(names3) #[1] 264 #lstNew <- simplify2array(ReadOut1) ###results you got # nrow(lstNew) #NULL#### ReadOut2 <- lapply(seq_along(ReadOut1),function(i) {df2 <- ReadOut1[[i]]; df3 <-as.data.frame(matrix(NA,nrow=101,ncol=length(names3), dimnames=list(NULL, names3))); names(df2) <- gsub("[.]"," ", gsub("\\_.*","", names(df2))); df2 <- df2[,-1]; df3[,match(names(df2), names(df3))] <- df2; df3}) lstNew <- simplify2array(ReadOut2) nrow(lstNew) #[1] 264 lapply(1:nrow(lstNew), function(i) { dat1 <- data.frame(Percentiles = paste0(seq(0, 100, by = 1), "%"), do.call(cbind, lstNew[i, ]), stringsAsFactors = FALSE); colnames(dat1) <- c("Percentiles", paste(names(lst3), rep(rownames(lstNew)[i],length(lst3)),sep="_")); write.csv(dat1,paste0(paste(getwd(), "Indices", gsub(" ", "_",rownames(lstNew)[i]), sep="/"),".csv"),row.names=FALSE, quote=FALSE)}) ## Output2: ReadOut2 <- lapply(list.files(recursive = TRUE)[grep("Indices", list.files(recursive = TRUE))], function(x) read.csv(x, header = TRUE, stringsAsFactors = FALSE)) length(ReadOut2) #[1] 264 ReadOut2[[1]][1:3,1:3] # Percentiles G100_pav.ANN G101_pav.ANN #1 0% 0.766900 0.96240 #2 1% 0.796132 0.96572 #3 2% 0.825364 0.96904