install.packages("data.table")
library("data.table")
pollutantmean <- function(directory, pollutant, id= 1:332){
## Create an empty vector of pollutants
pollutants = c()
## Get a list of filenames
filenames = list.files(directory)
## For each .csv file in id
for (i in id) {
## Concatinate the directory and filename
## e.g.
directory = "C:/Users/YUVRAJ/Downloads/rprog_data_specdata"
filenames = vector("001.csv", "002.csv",...)
filepath="C:/Users/YUVRAJ/Downloads/rprog_data_specdata/001.csv"
filepath=paste(directory,"/" , filenames[i], sep="")
## read in each file and store it in data
data = read.csv(filepath, header = TRUE)
##Concatinate the vectors from each file of the pollutant('sulfate' or 'nitrate') column to pollutants vector
pollutants = c(pollutants, data[,pollutant])
}
## Get the mean of the pollutants and remove NA values
pollutants_mean = mean(pollutants, na.rm=TRUE)
## Return the mean 'pollutants_mean'
pollutants_mean
}
错误消息:
Error in vector("001.csv", "002.csv", ...) :
'...' used in an incorrect context
您只能在两个设置中使用...
。 1.在编写自己的函数以允许更多用户定义的参数时。 2.将这些用户定义的参数传递给另一个函数时。此外,vector
的功能定义不支持...
。
[使用c
创建矢量时
filenames = c("001.csv", "002.csv")
您将删除错误。我不确定能否实现您想要的目标,因为我不清楚。