R code - Pollutant Mean is producing NaNs

五迷三道 提交于 2019-12-25 03:34:39

问题


I'm pretty close to finishing this R program, but the result keeps giving me NaN. It's supposed to find the nitrate or sulfate mean across a bunch of csv files. Would anyone know where the code might be going wrong? Below is the program description. It seems pretty self explanatory, it's just I'm somewhat stumped. If you need anymore details please let me know. Thanks

pollutantmean <- function(directory, pollutant, id = 1:332) {
        ## 'directory' is a character vector of length 1 indicating
        ## the location of the CSV files

        ## 'pollutant' is a character vector of length 1 indicating
        ## the name of the pollutant for which we will calculate the
        ## mean; either "sulfate" or "nitrate".

        ## 'id' is an integer vector indicating the monitor ID numbers
        ## to be used

        ## Return the mean of the pollutant across all monitors list
        ## in the 'id' vector (ignoring NA values)
}

pollutantmean = function(directory, pollutant, id = 1:332) {
            files_polm = list.files(directory, full.names = TRUE)
            dat_3 = numeric()
            for (x in id) {
                    dat_3 = rbind(dat_3, read.csv(files_polm[x]))
            }
            if (pollutant == "sulfate") {
                    sub_pol = dat_3[which(dat_3[, "sulfate"] == "sulfate"), ]
                    mean(sub_pol[, "sulfate"], na.rm = TRUE)
            }
            else if (pollutant == "nitrate") {
                    sub_pol = dat_3[which(dat_3[, "nitrate"] == "nitrate"), ]
                    mean(sub_pol[, "nitrate"], na.rm = TRUE)
            }
            else {
                    print("Try Again")
            }
    }

回答1:


I edited your code, assuming that within each .csv file your "nitrate" or "sulafte" column contains numerical or integer data type, i.e. the amount/concentration of each substance.

I also modified the for loop to be more coherent with your .csv files structure. Here is the code, hope it works - if not, please edit to indluce the output of str() function of one of your .csv files

pollutantmean = function(directory, pollutant, id = 1:332) {
 files_polm = list.files(directory, full.names = TRUE)
 dat_3 = numeric()
 for (x in id) {
   if (x==id[1]) {
     dat_3 = read.csv(files_polm[x])
   } else{
     dat_3 = rbind(dat_3, read.csv(files_polm[x])) 
   }

 }
 if (pollutant == "sulfate") {
    mean(sub_pol[, "sulfate"], na.rm = TRUE)
 } else if (pollutant == "nitrate") {
    mean(sub_pol[, "nitrate"], na.rm = TRUE)
 } else {
    print("Try Again")
 }
}


来源:https://stackoverflow.com/questions/29018579/r-code-pollutant-mean-is-producing-nans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!