我是编程的新手,我试图在R中编写一个函数,计算指定监视器列表中的污染物(硝酸盐或硫酸盐)的平均值(每个监视器在文件夹“specdata”中都有自己的.csv文件)。 我已经构建了以下功能:
pollutantmean <- function(directory="specdata", pollutant="sulfate", id=1:332) { files_f<-list.files(directory,full.names=TRUE) d <- data.frame() for(i in 1:332){ d <- rbind(d,read.csv(files_f[i])) } if(pollutant=="sulfate"){ mean(d$Sulfate[which(d$ID==id)], na.rm=TRUE) } else{ mean(d$Nitrate[which(d$ID==id)], na.rm=TRUE) } }然后我试着用下面的方法测试这个函数: pollutantmean(directory="specdata",pollutant="sulfate", id=1:10)
然后我得到以下错误:
[1] NA Warning messages: 1: In d$ID == id : longer object length is not a multiple of shorter object length 2: In mean.default(d$Sulfate[which(d$ID == id)], na.rm = TRUE) : argument is not numeric or logical: returning NA这是什么意思? 我已经多次查看了代码,但无法确定问题所在。
谢谢。
I am brand new to programming and am trying to write a function in R that calculates the mean of a pollutant (nitrate or sulfate) across a specified list of monitors (each monitor has its own .csv file in the folder "specdata"). I have constructed the following function:
pollutantmean <- function(directory="specdata", pollutant="sulfate", id=1:332) { files_f<-list.files(directory,full.names=TRUE) d <- data.frame() for(i in 1:332){ d <- rbind(d,read.csv(files_f[i])) } if(pollutant=="sulfate"){ mean(d$Sulfate[which(d$ID==id)], na.rm=TRUE) } else{ mean(d$Nitrate[which(d$ID==id)], na.rm=TRUE) } }And then I tried to test the function with: pollutantmean(directory="specdata",pollutant="sulfate", id=1:10)
I then get the following error:
[1] NA Warning messages: 1: In d$ID == id : longer object length is not a multiple of shorter object length 2: In mean.default(d$Sulfate[which(d$ID == id)], na.rm = TRUE) : argument is not numeric or logical: returning NAWhat does this mean? I've gone through my code many times but can't identify what the problem is.
Thank you.
最满意答案
在这里,我认为我已经在评论中实施了这些建议,缩短了代码,甚至概括了该功能,以防您想要调查其他污染物(只需确保将它们拼写成与csv相同,包括大写字母):
pollutantmean <- function(directory="specdata", pollutant="Sulfate", id=1:332){ files_f <- list.files(directory,full.names=TRUE) d <- do.call(rbind, lapply(files_f, read.csv, stringsAsFactors=FALSE)) mean(d[[pollutant]][which(d$ID %in% id)], na.rm=TRUE) }希望工程,污染物监测好运
Here I think I've implemented the suggestions in the comments, shortened the code, and even generalized the function in case you ever want to investigate other pollutants (just be sure to spell them the same as in the csv's, including capitalization):
pollutantmean <- function(directory="specdata", pollutant="Sulfate", id=1:332){ files_f <- list.files(directory,full.names=TRUE) d <- do.call(rbind, lapply(files_f, read.csv, stringsAsFactors=FALSE)) mean(d[[pollutant]][which(d$ID %in% id)], na.rm=TRUE) }Hope that works, good luck with the pollutant monitoring
更多推荐
发布评论