我是R.的新手.我在一个文件夹中有超过300个CSV文件(名为001.csv,002.csv等).每个包含一个带标题的数据框.我正在编写一个函数,它将采用三个参数:文件的位置,要计算平均值的列的名称(在数据框内)以及要在计算中使用的文件.
这是我的功能:
pollutantmean2 <- function(directory = getwd(), pollutant, id = 1:332) {
# add one or two zeros to ID so that they match the CSV file names
filenames <- sprintf("%03d.csv", id)
# path to specdata folder
# if no path is provided, default is working directory
filedir <- file.path(directory, filenames)
# get the data from selected ID or IDs from the specified path
dataset <- read.csv(filedir, header = TRUE)
# calculate mean removing all NAs
polmean <- mean(dataset$pollutant, na.rm …Run Code Online (Sandbox Code Playgroud)