R: Warning: get NA when I try to use my function -


i brand new programming , trying write function in r calculates mean of pollutant (nitrate or sulfate) across specified list of monitors (each monitor has own .csv file in folder "specdata"). have constructed following function:

pollutantmean <- function(directory="specdata", pollutant="sulfate", id=1:332) {     files_f<-list.files(directory,full.names=true)     d <- data.frame()      for(i in 1:332){          d <- rbind(d,read.csv(files_f[i]))      }     if(pollutant=="sulfate"){          mean(d$sulfate[which(d$id==id)], na.rm=true)      }     else{          mean(d$nitrate[which(d$id==id)], na.rm=true)      } } 

and tried test function with: pollutantmean(directory="specdata",pollutant="sulfate", id=1:10)

i following error:

[1] na warning messages: 1: in d$id == id : longer object length not multiple of shorter object length 2: in mean.default(d$sulfate[which(d$id == id)], na.rm = true) : argument not numeric or logical: returning na 

what mean? i've gone through code many times can't identify problem is.

thank you.

here think i've implemented suggestions in comments, shortened code, , generalized function in case ever want investigate other pollutants (just sure spell them same in csv's, including capitalization):

   pollutantmean <- function(directory="specdata",                                pollutant="sulfate",                                id=1:332){       files_f <- list.files(directory,full.names=true)       d <- do.call(rbind, lapply(files_f, read.csv, stringsasfactors=false))       mean(d[[pollutant]][which(d$id %in% id)], na.rm=true)     } 

hope works, luck pollutant monitoring


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -