R:警告:当我尝试使用我的功能时得到NA(R: Warning: get NA when I try to use my function)

编程入门 行业动态 更新时间:2024-10-23 22:37:40
R:警告:当我尝试使用我的功能时得到NA(R: Warning: get NA when I try to use my function)

我是编程的新手,我试图在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 NA

What 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

更多推荐

本文发布于:2023-04-27 23:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329667.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我   功能   NA   Warning   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!