Calculate the mean of one column from several CSV files

前端 未结 2 2071
天命终不由人
天命终不由人 2020-12-01 15:27

I have over 300 CSV files in a folder (named 001.csv, 002.csv and so on). Each contains a data frame with a header. I am writing a function that will take three arguments: t

相关标签:
2条回答
  • 2020-12-01 15:54

    You do not need more functions. The solution can be simpler from my understanding in 6 lines:

    pollutantmean <- function(directory, pollutant, id = 1:10) {
    filenames <- sprintf("%03d.csv", id)
    filenames <- paste(directory, filenames, sep="/")
    ldf <- lapply(filenames, read.csv)
    df=ldply(ldf)
    # df is your list of data.frames
    mean(df[, pollutant], na.rm = TRUE)
    }
    
    0 讨论(0)
  • 2020-12-01 16:08

    I think your major problem is listing the files in your working directory and reading them into R. Try list.files function in R Example code which may work for you is

      files <- list.files(pattern = ".csv") ## creates a vector with all file names in your folder
    polmean <- rep(0,length(files))
    for(i in 1:length(files)){
       data <- read.csv(files[i],header=T)
       polmean[i] <- mean(data$pollutant)
     }
    result <- cbind(files,polmean)
    write.csv(result,"result_polmeans.csv")
    

    This program gives you the data with name of file in the first column and corresponding means in the second column.

    0 讨论(0)
提交回复
热议问题