0

I am using the function below but I got wrong and the same answers to the when I call do functions. Could you please assist? What do I do wrong ?

pollutantmean <- function(directory, pollutant, id){
    directory<- list.files("specdata", full.names = TRUE)
    id <- 1:332
    getmean <- data.frame(check.rows = TRUE)
    for(i in seq_along(id)) {
    getmean<- rbind(getmean,read.csv(directory[i], blank.lines.skip = TRUE))}
    pollutant <- c("sulfate", "nitrate")
    for (i in seq_along(pollutant)) { poll <- i}
    mean(getmean[ , poll], na.rm = TRUE)
    }

Then I call

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

I got

# [1] 3.189369

when I call pollutantmean("specdata", "sulfate", 23), I got the same answer

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
sensar
  • 9
  • 1

1 Answers1

1

There are several things that I notice that are wrong with your program.

  1. Your directory will always be the same regardless of what argument you give for directory.
  2. The seq_along is not necessary in your first for loop because is already a sequence starting at 1!
  3. And most importantly, your for loop affects poll but it does not affect the mean function call. That means, poll always is set to 1, then to 2 and the for loop ends. Your mean call will always call with poll set to 2. That's why you get the same answer regardless.
  4. This is a side note but your arguments don't actually do anything with your program as I further study your code. You set the values to the variables inside of your function.
PhillipAMann
  • 887
  • 1
  • 10
  • 19