1
votes

I have following code for getting the pollutant mean. The data for the program are in folder specdata with names "001.csv", "002.csv", ... to "332.csv" . The names of any one data gives following names:

[1] "Date"    "sulfate" "nitrate" "ID"  

In the code below, I have to calculate the mean of pollutant nitrate or sulfate . I think the code is correct. But data$pollutant is giving

NULL

Error in pollutantmean("specdata", "sulfate", 23) : attempt to apply non-function

The code is supposed to call in following way:

pollutantmean("specdata", "nitrate", 23)

What am I doing wrong here??

pollutantmean <- function(directory, pollutant, id = 1:332) {
  f <- function(num){
    if(num>=0 & num<=9){
      fname <- paste('specdata/00',as.character(num),".csv",sep="")
    }
    else if (num>=10 & num <=99){
      fname <- paste('specdata/0',as.character(num),".csv",sep="")
    }
    else{
      fname <- paste('specdata/',as.character(num),".csv",sep="")
    }
    data <- read.csv(fname)
    data <- data[complete.cases(data),]
    return(mean(data$pollutant))
  }
  results <- sapply(id, f)
  return(results)

}
1
Not related to your question, but useful hint: run sprintf("%03.f", 1:322).tonytonov
ah thanks! this could have saved me in looping above. I learning R. This is good and shortcut method. ThanksPant
Minor point: you also hard-code "specdata/" and never use directory.r2evans

1 Answers

0
votes

The error you have is most probably caused by calling data$pollutant. You have pollutant="nitrate", which is a character string, in that case reference by $ is not going to work. Use [] instead, here's a minimal example:

df <- data.frame(hello=1:3, world=5)
name <- "hello"
df$name
NULL
df[name]
#  hello
#1     1
#2     2
#3     3