That's the way I fixed it:
pollutantmean <- function(directory, pollutant, id = 1:332) {
#set the path
path = directory
#get the file List in that directory
fileList = list.files(path)
#extract the file names and store as numeric for comparison
file.names = as.numeric(sub("\\.csv$","",fileList))
#select files to be imported based on the user input or default
selected.files = fileList[match(id,file.names)]
#import data
Data = lapply(file.path(path,selected.files),read.csv)
#convert into data frame
Data = do.call(rbind.data.frame,Data)
#calculate mean
mean(Data[,pollutant],na.rm=TRUE)
}
The last question is that my function should call "specdata" (the directory name where all the csv's are located) as the directory, is there a directory type object in r?
suppose i call the function as:
pollutantmean(specdata, "niterate", 1:10)
It should get the path of specdata directory which is on my working directory... how can I do that?