本文介绍了R代码 - Pollutant Mean正在生产NaNs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我非常接近完成这个R程序,但结果却一直给我NaN。它应该在一堆csv文件中找到硝酸盐或硫酸盐的含义。有人会知道代码可能出错吗?以下是程序说明。这似乎很自我解释,这只是我有点难住。如果您需要更多详情,请告诉我。谢谢 pollutantmean ##'directory '是长度为1的字符矢量,表示 ## CSV文件的位置 ##'污染物'是长度为1的字符矢量,表示 ##名称我们将计算 ##的平均值; 硫酸盐或硝酸盐。 ##'id'是一个整数向量,指示监视器ID号 ##被使用 ##返回所有监视器中污染物的平均值在'id'向量中列出 ##(忽略NA值)} pollutantmean =函数(目录,污染物,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 [ (污染物==硫酸盐){ 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)} 其他{ print(再试一次)} } / p> 我还修改了for循环,使其与.csv文件结构更加一致。这里是代码,希望它能起作用 - 如果没有,请编辑以释放您的某个.csv文件的 str()函数的输出结果 pollutantmean =函数(目录,污染物,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(污染物== 硫酸盐){ mean(sub_pol [,sulfate],na.rm = TRUE)} else if(pollutant ==nitrate){ mean(sub_pol [ ),na.rm = TRUE)} else { print(再试一次)} } 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. Thankspollutantmean <- 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") } } 解决方案 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") }} 这篇关于R代码 - Pollutant Mean正在生产NaNs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-22 12:51