function not working R
- by user3722828
I've never programmed before and am trying to learn. I'm following that "coursera" course that I've seen other people post about — a course offered by Johns Hopkins on R programming.
Anyway, this was supposed to be my first function. Yet, it doesn't work! But when I type out all the steps individually, it runs just fine... Can anyone tell me why?
> pollutantmean <- function(directory, pollutant, id = 1:332){
+ x<- list.files("/Users/mike******/Desktop/directory", full.names=TRUE)
+ y<- lapply(x, read.csv)
+ z<- do.call(rbind.data.frame, y[id])
+
+ mean(z$pollutant, na.rm=TRUE)
+ }
> pollutantmean(specdata,nitrate,1:10)
[1] NA
Warning message:
In mean.default(z$pollutant, na.rm = TRUE) :
argument is not numeric or logical: returning NA
####
> x<- list.files("/Users/mike******/Desktop/specdata",full.names=TRUE)
> y<- lapply(x,read.csv)
> z<- do.call(rbind.data.frame,y[1:10])
> mean(z$nitrate,na.rm=TRUE)
[1] 0.7976266