0

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

  • The linked duplicate goes over this in more detail, but you're looking for `data[,pollutant]`. – josliber Mar 06 '16 at 01:46
  • Or `data[[pollutant]]`. This isn't really a duplicate of the linked question, since this is asking about `data.frame`s and the other question is about matrices and extraction works differently. – Ista Mar 06 '16 at 03:29

0 Answers0