Function output NaN value (Linux)

281 views Asked by At

I'm writing a function to calculate the mean of a given pollutant. Here is my function:

pollutantmean <- function(directory, pollutant, id = 1:332) {
files <- list.files(directory, full.names = TRUE) #creates a list of files
dat <- data.frame()
    for (i in 1:332) {

        data <- rbind(dat, read.csv(files[i])) #loops through the files and created a data frame
    }
data_1 <- data[which(data[, "ID"] %in% id), ]

data_subset_n <- data_1[which(data_1[, "nitrate"] == "nitrate"), ]
data_subset_s <- data_1[which(data_1[, "sulfate"] == "sulfate"), ]
data_2 <- cbind(data_subset_s, data_subset_n) #binds the two pollutants
mean((data_2[ ,pollutant]), na.rm=TRUE) #calculates the mean of the pollutant

}

I have gone through it many times and have no idea of what might go wrong, but my function output is still NaN.

> pollutantmean("specdata", "sulfate", 1:10)
[1] NaN

Please help.

Thank You.

1

There are 1 answers

0
modulus On

Never mind I found the answer. It was in the for loop that I assigned the value to 'data' instead of 'dat'.