When you call
pollutantmean("specdata","sulfate", 1:10)
the variable directory within pollutantmean gets the value "specdata". So,
- There is no need to set the value of directory to be "specdata" within the function.
- When you call
z <- list.files("directory")
the term directory should not be in quotes.
Also, it is a bad idea to manually increment the i variable within the for loop. Let the for loop do the incrementing.
Finally, I do not think you want to write
mean(totaal[ind], na.rm == FALSE)
because ind is a vector. Don't you want to use the parameter pollutant there? And your title says you want to ignore NA, so use na.rm = TRUE.
I would write the function like this.
pollutantmean <- function(directory, pollutant, ind)
{
z <- list.files(directory)
totaal <- data.frame()
for (i in ind){
hulpspec <- read.table(z[i])
totaal <- rbind(totaal,hulpspec)
}
mean(totaal[, pollutant], na.rm = TRUE)
}
I have not tested that, since I do not have your data, so it may have mistakes.