- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在将一系列集水多边形(来自 shapefile)叠加到 SpatialPixelsDataFrame 对象上时,我遇到了一个奇怪的问题。有 70 个集水区,但我得到了 NA
s 用于 60 个集水区。
数据在 Dropbox 上可用,以下脚本将下载、解压缩文件等,并创建一个可重现的示例。
## load packages required
require(rgdal)
require(sp)
## proj4 string
BNG <- CRS("+init=epsg:27700")
## load in the grid
download.file("https://www.dropbox.com/s/7dvcf6ddo6r3043/depogrid.rds?dl=1",
"./depogrid.rds", method = "wget")
myGrid <- readRDS("depogrid.rds")
## convert to spatial pixels
depo.sp <- SpatialPixelsDataFrame(data.matrix(myGrid[, 1:2]),
data = myGrid[, 3, drop = FALSE],
proj4string = BNG)
## load the shapefile
download.file("https://www.dropbox.com/s/bwzmeumnrja3c0o/dart.zip?dl=1",
"./dart.zip", method = "wget")
unzip("./dart.zip")
shp <- readOGR(".", "DARTall")
## project shp to OSGB36
shp <- spTransform(shp, BNG)
## overlay shapefile on depo grid
depo.dart <- over(shp, depo.sp)
NA
s:
> head(depo.dart)
Deposition
0 NA
1 NA
2 NA
3 NA
4 NA
5 0.2604089
> sum(is.na(depo.dart[,1]))
[1] 60
> nrow(depo.dart)
[1] 70
NA
s 因为网格覆盖了整个英国地区并且集水区很小,所以我可能做错了什么!如果是这样,我做错了什么,我该如何解决?我想得到
Deposition
的平均值(
SpatialPixelsDataFrame
数据)用于
shp
中的 70 个流域中的每个流域.
depo.sp
相同.
> over(shp, depo.sp, returnList = TRUE)
[[1]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[2]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[3]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[4]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[5]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[6]]
Deposition
3143 0.2604089
[[7]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[8]]
Deposition
3289 0.1139663
[[9]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[10]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[11]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[12]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[13]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[14]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[15]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[16]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[17]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[18]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[19]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[20]]
Deposition
3585 0.8610678
[[21]]
Deposition
3288 0.2242067
[[22]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[23]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[24]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[25]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[26]]
Deposition
3141 0.3296081
[[27]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[28]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[29]]
Deposition
3438 0.3609788
[[30]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[31]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[32]]
Deposition
3287 0.4812801
[[33]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[34]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[35]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[36]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[37]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[38]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[39]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[40]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[41]]
Deposition
3436 0.3223757
[[42]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[43]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[44]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[45]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[46]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[47]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[48]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[49]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[50]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[51]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[52]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[53]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[54]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[55]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[56]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[57]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[58]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[59]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[60]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[61]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[62]]
Deposition
2580 0.2820617
[[63]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[64]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[65]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[66]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[67]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[68]]
Deposition
3141 0.3296081
3287 0.4812801
3288 0.2242067
3438 0.3609788
[[69]]
[1] Deposition
<0 rows> (or 0-length row.names)
[[70]]
[1] Deposition
<0 rows> (or 0-length row.names)
> str(over(shp, depo.sp, returnList = TRUE))
List of 70
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 1 obs. of 1 variable:
..$ Deposition: num 0.26
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 1 obs. of 1 variable:
..$ Deposition: num 0.114
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 1 obs. of 1 variable:
..$ Deposition: num 0.861
$ :'data.frame': 1 obs. of 1 variable:
..$ Deposition: num 0.224
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 1 obs. of 1 variable:
..$ Deposition: num 0.33
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 1 obs. of 1 variable:
..$ Deposition: num 0.361
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 1 obs. of 1 variable:
..$ Deposition: num 0.481
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 1 obs. of 1 variable:
..$ Deposition: num 0.322
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 1 obs. of 1 variable:
..$ Deposition: num 0.282
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 4 obs. of 1 variable:
..$ Deposition: num [1:4] 0.33 0.481 0.224 0.361
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
$ :'data.frame': 0 obs. of 1 variable:
..$ Deposition: num(0)
最佳答案
如果您愿意进行实验并愿意使用 raster
包,这是一个可能的解决方案。让我们知道。
诀窍是,在栅格上覆盖多边形时,指定 small = TRUE
,因为多边形的中心并不总是包括单元中心(见 ?extract
)。
If y represents polygons, a value is also returned for relatively small polygons (e.g. those smaller than a single cell of the Raster* object), or polygons with an odd shape, for which otherwise no values are returned because they do not cover any raster cell centers.
library(raster)
shp.raster <- raster(shp)
depo.sp.raster <- raster(depo.sp)
plot(depo.sp.raster)
plot(shp, add = TRUE)
zoom(depo.sp.raster) # click in the corners around the area to zoom in
plot(shp, add = TRUE)
res.out <- extract(depo.sp.raster, shp, layer = 1, small = TRUE)
table(unlist(lapply(res.out, is.null)))
> head(res.out)
[[1]]
0.0334963
[[2]]
0.2604089
[[3]]
[1] 0.5466648 0.7929624 0.4214012
[[4]]
[1] 0.2604089 0.3149418 0.5343358 0.0334963
[[5]]
0.5561869
[[6]]
0.2604089
关于r - 在 SpatialPixelsDataFrame 上叠加 shapefile 时产生的 NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13565179/
在 R Language Definition 中,对NA值进行了简要描述,其中一部分说 ... In particular, FALSE & NA is FALSE, TRUE | NA is TR
我对 R 还很陌生,目前遇到一个问题,数据如下所示: ID h1 h2 h3 h4 h5 h6 h7 h8
我有一个 csv包含国家名称及其 ISO 代码的文件。这是它的样子: "Name","Code" "Afghanistan","AF" "Albania","AL" "Algeria","DZ" "N
我想用 dplyr 解决以下问题。最好与窗口功能之一一起使用。我有一个包含房屋和购买价格的数据框。下面是一个例子: houseID year price 1 19
在data.frame(或data.table)中,我想用最接近的先前非NA值“向前填充”NA。一个使用向量(而不是 data.frame)的简单示例如下: > y yy [1] NA NA NA
这是一个示例数据框: > df = data.frame(rep(seq(0, 120, length.out=6), times = 2), c(sample(1:50, 4), + NA, NA,
我有一个包含条目的数据框;似乎这些值不被视为 NA,因为 is.na 返回 FALSE。我想将这些值转换为 NA 但找不到方法。 最佳答案 使用 dfr[dfr==""]=NA哪里dfr是你的数据框。
我有一个示例表,其中包含一些但不是全部 NA需要替换的值。 > dat id message index 1 1 1 2 1 foo 2 3 1
在 R 中,如果从 NA 中减去一个数字,它将返回 NA: > x NA - x [1] NA 但是如果你尝试从 NA 中减去一个日期,它会返回一个错误: > x NA - x Error in
这个问题在这里已经有了答案: Logical operators (AND, OR) with NA, TRUE and FALSE (2 个答案) 关闭 4 年前。 为什么在 R 中会这样? >
我有一个看起来像这样的数据框: SampleNo Lab1 Lab2 Lab3 lab4 lab5 lab6 lab7 lab8 lab9 lab10 1 59
我有一个按“id”分组的数据框和一个包含缺失值的变量“age”,NA。 在每个“id”中,我想替换“age”的缺失值,但只“填充”之前 第一个 非NA 值。 data % group_by(id) %
我有如下所示的数据框: df df id value v1 v2 v3 1 1 351 NA 1 0 2 2 585 0 1 1 3 3 321 NA 0 1 4
所以我有一个数据集,只需查看它,数据集中就有明显的 NA。 > dput(bmi.cig) structure(list(MSI.subset.BMI = structure(c(4L, 4L, 4
我有两个 30m x 30m 的光栅文件,我想从中采样点。在采样之前,我想从图像中移除模糊区域。我求助于 R 和 Hijman 的 Raster 包来完成这项任务。 使用 drawPoly(sp=TR
我有以下时间序列 > y y[c(1,2,5,9,10)] y [,1] 2011-09-04 NA 2011-09-05 NA 2011-09-06 3 201
这个问题在这里已经有了答案: Replace missing values (NA) with most recent non-NA by group (7 个回答) 5年前关闭。 我有一个 DF 个
我想向我的数据框中添加一个新变量 (N_notNAs),它定义了其他任何变量是否为 NA。 x y z N_notNAs 2 3 NA NA NA 1 3 NA 2
我有一个名为 SMOKE 的因子,级别为“Y”和“N”。缺失值被替换为 NA(从初始级别“NULL”开始)。然而,当我查看这个因素时,我得到这样的结果: head(SMOKE) # N N Y Y
假设我有以下 data.frame: t<-c(1,1,2,4,5,4) u<-c(1,3,4,5,4,2) v<-c(2,3,4,5,NA,2) w<-c(NA,3,4,5,2,3) x<-c(2,
我是一名优秀的程序员,十分优秀!