R, rbind with multiple files defined by a variable

前端 未结 2 2082
滥情空心
滥情空心 2021-01-27 09:21

First off, this is related to a homework question for the Coursera R programming course. I have found other ways to do what I want to do but my research has led me to a question

2条回答
  •  梦如初夏
    2021-01-27 09:51

    A vector is not accepted for 'file' in read.csv(file, ...)

    Below is a slight modification of yours. A vector of file paths are created and they are looped by sapply.

    files <- paste("directory-name/",formatC(1:332, width=3, flag="0"),
                   ".csv",sep="")
    pollutantmean <- function(file, pollutant) {
        dataset <- read.csv(file, header = TRUE)
        mean(dataset[, pollutant], na.rm = TRUE)
    }
    sapply(files, pollutantmean)
    

提交回复
热议问题