r - Calculate the mean of one column across multiple .csv files How? -
i newbie in r , have got calculate mean of column sulf
332 files. mean formulas bellow works 1 file . problem comes when attempt calculate across files.
perhaps reading files , storing them in mydata
not work well? me out?
many thanks
pollutantmean <- function(specdata,pollutant=xor(sulf,nit),i=1:332){ specdata<-getwd() pollutant<-c(sulf,nit) for(i in 1:332){ mydata<-read.csv(file_list[i]) } sulfate <- (subset(mydata,select=c("sulfate"))) sulf <- sulfate[!is.na(sulfate)] y <- mean(sulf) print(y) }
this not tested, steps followed. note kind of questions being asked on , on again (e.g. here). try searching "work on multiple files", "batch processing", "import many files" or akin this.
lx <- list.files(pattern = ".csv", full.names = true) # gives list of xy <- sapply(lx, fun = function(x) { out <- read.csv(x) out <- out[, "sulfate", drop = false] # not drop vector fun out <- out[is.na(out[, "sulfate"]), ] out }, simplify = false) xy <- do.call(rbind, xy) # combine result files 1 big data.frame mean(xy[, "sulfate"]) # calculate mean # or summary(xy)
if short on ram, can optimized bit.
Comments
Post a Comment