I am trying to calculate the mean of a subset of my dataframe. However, I have found that my subset returns to be of class 'closure' while I just want it to be a vector. The head of my df may look like:
Date sulfate nitrate ID
1 2002-01-01 NA NA 8
2 2002-01-02 NA NA 8
3 2002-01-03 NA NA 8
4 2002-01-04 NA NA 8
5 2002-01-05 NA NA 8
6 2002-01-06 NA NA 8
There are non NA values in both "sulfate" and "nitrate" further down the df.
I have tried to subset using freem[pollutant] rather than freem$pollutant. This doesn't seem to make any difference.
pollutantmean <- function(directory, pollutant, id = 1:332) {
means <- c()
for(i in id) {
x <- paste(getwd(), "/", directory, "/", sprintf("%03d", i), ".csv", sep = "")
freem <- read.csv(x)
inte <- freem$pollutant
print(class(frame$pollutant))
means[i] <- mean(inte, na.rm = TRUE)
}
mean(means)
}
I expect this for loop to fill the empty vector means
with the means of the subsets of all selected monitors
(basically different csv files in my wd)