I have following code for getting the pollutant mean. The data for the program are in folder specdata with names "001.csv", "002.csv", ... to "332.csv" . The names of any one data gives following names:
[1] "Date" "sulfate" "nitrate" "ID"
In the code below, I have to calculate the mean of pollutant nitrate or sulfate . I think the code is correct. But data$pollutant is giving
NULL
Error in pollutantmean("specdata", "sulfate", 23) : attempt to apply non-function
The code is supposed to call in following way:
pollutantmean("specdata", "nitrate", 23)
What am I doing wrong here??
pollutantmean <- function(directory, pollutant, id = 1:332) {
f <- function(num){
if(num>=0 & num<=9){
fname <- paste('specdata/00',as.character(num),".csv",sep="")
}
else if (num>=10 & num <=99){
fname <- paste('specdata/0',as.character(num),".csv",sep="")
}
else{
fname <- paste('specdata/',as.character(num),".csv",sep="")
}
data <- read.csv(fname)
data <- data[complete.cases(data),]
return(mean(data$pollutant))
}
results <- sapply(id, f)
return(results)
}
sprintf("%03.f", 1:322)
. – tonytonov"specdata/"
and never usedirectory
. – r2evans