sp R 包和缺失值

发布于 2024-12-15 16:43:13 字数 463 浏览 1 评论 0原文

我正在尝试使用 sp 包在 R 中创建气泡图。我的数据有很多缺失值(“N/A”),并且气泡图似乎不喜欢它。

library(sp)
X<-runif(100, min=0, max=1000)
Y<-runif(100, min=0, max=1000)
grade<-c((rnorm(n=50,mean=30, sd=4)), (rep(NA, 50)))
df<-data.frame(X,Y, grade)
coordinates(df)<-~X+Y
bubble(df, "grade", na.rm=TRUE)

当我运行此代码时,我收到一条错误消息“Quantile.default(data[, zcol]) 中的错误:缺少值,并且如果 'na.rm' 为 FALSE,则不允许使用 NaN”。

我不明白,因为我说过要删除缺失值!我怀疑 sp 有一个稍微不同的方法来处理我错过的缺失值

I am trying to create a bubble plot in R using the sp package. My data has a lot of missing values ("N/A") and the bubble plot does not seem to like it.

library(sp)
X<-runif(100, min=0, max=1000)
Y<-runif(100, min=0, max=1000)
grade<-c((rnorm(n=50,mean=30, sd=4)), (rep(NA, 50)))
df<-data.frame(X,Y, grade)
coordinates(df)<-~X+Y
bubble(df, "grade", na.rm=TRUE)

When I run this code I am getting an error message "Error in quantile.default(data[, zcol]) : missing values and NaN's not allowed if 'na.rm' is FALSE".

I don't understand because I have said to remove missing values!! I suspect that sp has a slightly different method for dealing with missing values that I have missed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光飞翔i 2024-12-22 16:43:13

试试这个(因为 na.rm 似乎没有传递给正确的函数):

bubble(df[!is.na(df$grade), ], "grade")

还有一个用于“SpatialPointsDataFrame”对象的子集方法:

bubble(subset(df, !is.na(grade)), "grade")

Try this instead (since the na.rm does not seem to get passed to the proper function):

bubble(df[!is.na(df$grade), ], "grade")

There is also a subset method for 'SpatialPointsDataFrame'-objects:

bubble(subset(df, !is.na(grade)), "grade")
開玄 2024-12-22 16:43:13

如果您阅读 help(bubble),您会发现没有 na.rm 参数。仅仅因为它适用于 lm 和 glm 并不意味着它在任何地方都适用。请记住,R 是由数百人编写的,并且没有普遍要求遵循 na.rm 始终有效的某些规则。

请注意, bubble 有一个“...”参数 - 这将捕获您的 'na.rm' 并将其传递给 xyplot - 但也没有 na.rm 参数。这并不重要,因为错误是在代码考虑调用 xyplot 之前就在气泡中抛出的。

子集就是答案(如前所述)

If you read help(bubble) you'll see that there's no na.rm parameter. Just because it works with lm and glm that doesn't mean it will work everywhere. Remember that R is written by hundreds of people and there's no universal requirement to follow some rule that na.rm always works.

Note that bubble has a "..." argument - this will catch your 'na.rm' and pass it on to xyplot - but that doesn't have an na.rm argument either. Not that that matters since the error is thrown up by code in bubble before it even thinks about calling xyplot.

subset is the answer (as already explained)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文