function not producing correct mean output in R -


i working on homework assignment r programming course on coursera. have directory weather data readings 332 monitors. 172,385 observations of 4 variables. variables or date, sulfate, nitrate, , monitor id. i've written function below , gotten work, not returning correct values. first real experience programming , it's homework assignment i'm looking hints or advice on i'm doing wrong.

here function:

polutantmean14 <- function(directory, polutant = "nitrate", id = 1:332) {   files_list <- list.files(directory, full.names = true)   dat <- data.frame()   ( in 1:332) {     dat <- rbind(dat, read.csv(files_list[i]))   }   dat_subset <- dat[which(dat[, "id"] == id), ]   mean(dat_subset[, polutant], na.rm= true) } 

here error get:

polutantmean14("specdata", polutant = "sulfate", id = 1:10)

[1] 3.838328

warning message: in dat[, "id"] == id : longer object length not multiple of shorter object length

the function returns 3.838328 given arguments supposed 4.064.

like mentioned earlier, brand new programming , homework i'm looking advice , guidance on error is.

try using %in% instead of ==.


Comments