I am quite new to the R area. During an assignment "Air pollution" in Coursera, I tried to write a function that returned the mean to the accumulated dataset.
file <- list.files(directory, full.names= TRUE)
data <- data.frame()
for(val in id) {
data <- rbind(data, read.csv(file[val]))
}
mean(data[[pollutant]], na.rm = TRUE)
}
pollutantmean("specdata", "sulfate", 1:20)
And I got the answer 3.016321, which was right. However, when I changed the subset code to
mean(data$pollutant, na.rm = TRUE
This returned an error
Error in <my code> : object of type 'closure' is not subsettable
Why did this happen? And is there possibly a correct way to subset the data using $ in this code? I tried to search for the answer for myself but still could not figure out the solution.
Thank you for your kind answer.