如何替换光栅对象中的 NA [英] How to replace NA's in a raster object

查看:33
本文介绍了如何替换光栅对象中的 NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要替换下面示例中光栅对象 (r) 中的 NA.

I need to replace the NA's in the raster object (r) from the example below.

library(raster)
filename <- system.file("external/test.grd", package="raster")
r <- raster(filename)

我也尝试删除这些(并将结果放在 data.frame 中),但无济于事.

I also tried to remove these these (and place the result in a data.frame), but to no avail.

dfr <- as.data.frame(r, na.rm=T)
summary(dfr)
# test       
# Min.   : 128.4  
# 1st Qu.: 293.2  
# Median : 371.4  
# Mean   : 423.2  
# 3rd Qu.: 499.8  
# Max.   :1805.8  
# NA's   :6097

推荐答案

我不确定从 raster 对象中删除 NA 值是否有意义,但是您可以轻松更换.

I'm not sure it makes sense to remove NA values from a raster object, but you can easily replace it.

例如:

oldpar <- par(mfrow=c(1, 2))
plot(r)
r[is.na(r)] <- 250
plot(r)
par(oldpar)

如果你真的想要,你可以将栅格值提取到一个向量中,然后删除 NA 值.(不过,由于您丢失了空间信息,我看不出这有什么帮助.)

If you really want to, you can extract the raster values into a vector and then remove the NA values. (Although, since you lose the spatial information, I can't see how this can be helpful.)

r <- raster(filename)

r <- values(r)
head(r)
[1] NA NA NA NA NA NA

head(na.omit(r))
[1] 633.686 712.545 654.162 604.442 857.256 755.506

这篇关于如何替换光栅对象中的 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆