function not working R
Posted
by
user3722828
on Stack Overflow
See other posts from Stack Overflow
or by user3722828
Published on 2014-06-09T15:11:04Z
Indexed on
2014/06/09
15:25 UTC
Read the original article
Hit count: 746
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
© Stack Overflow or respective owner