In the below function I reference the dataframe "data", which has two columns called "nitrate" and "dust". The pollutant argument is a string (either "nitrate" or "dust".) But for some reason when I call the function I run into a problem with the "data$pollutant" line. Likewise,
pollutant <- "nitrate"
data$pollutant
Throws an error. But,
data$nitrate
Works fine
is.na() applied to non-(list or vector) of type 'NULL'
What am I doing wrong?
> pollutantmean <- function(directory, pollutant, id = 1:332)
{
sum = 0
length = 0
listCSV <- list.files()[id]
for (i in listCSV)
{
data <- read.csv(i)
no_missing <- data[!is.na(data$pollutant),]
sum = sum + sum(no_missing$pollutant)
length = length + length(no_missing$pollulant)
}
sum/length
}
Thanks! -Steve