Calculate the mean of one column from several CSV files

前端 未结 2 2070
天命终不由人
天命终不由人 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)
    }
    

提交回复
热议问题