- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当点在指定距离内时,我想创建 SpatialPointsDataFrame 的所有变量的平均值。我有一种方法可以做到这一点,但这似乎是一种解决问题的愚蠢方法。任何使用 tidy 种类的现代语法来做到这一点的想法都将不胜感激。
首先,我有一个 SpatialPointsDataFrame
每个点测量几个变量。我想获得指定距离内点的所有变量的平均值。例如,从 meuse
中获取平均镉值彼此相距 100 m 内的点的数据:
library(sf)
library(sp)
data(meuse)
pts <- st_as_sf(meuse, coords = c("x", "y"),remove=FALSE)
pts100 <- st_is_within_distance(pts, dist = 100)
# can use sapply to get mean of a variable. E.g., cadmium
sapply(pts100, function(x){ mean(pts$cadmium[x]) })
sapply
一个变量来做这个变量。因此,如果我愿意,我可以计算每个变量的平均值,为每个点生成一个质心,然后生成一个
SpatialPointsDataFrame
的独特值(value)。例如,对于前几个变量:
res <- data.frame(id=1:length(pts100),
x=NA, y=NA,
cadmium=NA, copper=NA, lead=NA)
res$x <- sapply(pts100, function(p){ mean(pts$x[p]) })
res$y <- sapply(pts100, function(p){ mean(pts$y[p]) })
res$cadmium <- sapply(pts100, function(p){ mean(pts$cadmium[p]) })
res$copper <- sapply(pts100, function(p){ mean(pts$copper[p]) })
res$lead <- sapply(pts100, function(p){ mean(pts$lead[p]) })
res2 <- res[duplicated(res$cadmium),]
coordinates(res2) <- c("x","y")
bubble(res2,"cadmium")
最佳答案
看起来 sf
有一个聚合函数具有连接参数的包,您可以在其中指定连接类型。
ibrary(sf)
library(sp)
data(meuse)
pts <- st_as_sf(meuse, coords = c("x", "y"),remove=FALSE)
# This will give lots of warnings since there are non-numeric columns
pts_agg <- aggregate(pts,
pts,
FUN = mean,
join = function(x, y) st_is_within_distance(x, y, dist = 100))
head(pts_agg)
Simple feature collection with 6 features and 14 fields
geometry type: POINT
dimension: XY
bbox: xmin: 181025 ymin: 333260 xmax: 181390 ymax: 333611
CRS: NA
x y cadmium copper lead zinc elev dist om ffreq soil lime landuse dist.m
1 181048.5 333584.5 10.15 83 288 1081.5 7.446 0.006791165 13.8 NA NA NA NA 40
2 181048.5 333584.5 10.15 83 288 1081.5 7.446 0.006791165 13.8 NA NA NA NA 40
3 181165.0 333537.0 6.50 68 199 640.0 7.800 0.103029000 13.0 NA NA NA NA 150
4 181298.0 333484.0 2.60 81 116 257.0 7.655 0.190094000 8.0 NA NA NA NA 270
5 181307.0 333330.0 2.80 48 117 269.0 7.480 0.277090000 8.7 NA NA NA NA 380
6 181390.0 333260.0 3.00 61 137 281.0 7.791 0.364067000 7.8 NA NA NA NA 470
geometry
1 POINT (181072 333611)
2 POINT (181025 333558)
3 POINT (181165 333537)
4 POINT (181298 333484)
5 POINT (181307 333330)
6 POINT (181390 333260)
抽查第 9 行的 pts,因为它在 pts100 中有几个匹配项:
> pts[pts100[[9]], 'cadmium'] %>% st_drop_geometry %>% summarise(mean = mean(cadmium))
mean
1 2.25
> pts_agg[9,'cadmium']
Simple feature collection with 1 feature and 1 field
geometry type: POINT
dimension: XY
bbox: xmin: 181060 ymin: 333231 xmax: 181060 ymax: 333231
CRS: NA
cadmium geometry
9 2.25 POINT (181060 333231)
关于r - 根据距离聚合 sf 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55110443/
我有一个 Cassandra 集群,里面有 4 个表和数据。 我想使用聚合函数(sum,max ...)发出请求,但我在这里读到这是不可能的: http://www.datastax.com/docu
我有以下两张表 Table: items ID | TITLE 249 | One 250 | Two 251 | Three 我投票给这些: Table: votes VID | IID | u
这个问题在这里已经有了答案: Update MongoDB field using value of another field (12 个答案) 关闭 3 年前。 我想根据另一个“源”集合的文档中
我的收藏包含以下文件。我想使用聚合来计算里面有多少客户,但我遇到了一些问题。我可以获得总行数,但不能获得总(唯一)客户。 [{ _id: "n001", channel: "Kalip
我有下表 Id Letter 1001 A 1001 H 1001 H 1001 H 1001 B 1001 H 1001 H 1001
得到一列的表 ABC。 “创建”的日期列。所以样本值就像; created 2009-06-18 13:56:00 2009-06-18 12:56:00 2009-06-17 14:02:0
我有一个带有数组字段的集合: {[ name:String buyPrice:Int sellPrice:Int ]} 我试图找到最低和最高买入/卖出价格。在某些条目中,买入或卖出价格为零
我有以下问题: 在我的 mongo db 中,我有以下结构: { "instanceId": "12", "eventId": "0-1b", "activityType":
下面给出的是我要在其上触发聚合查询的 Elasticsearch 文档。 { "id": 1, "attributes": [ { "fieldId": 1,
我正在使用 Django 的 aggregate query expression总计一些值。最终值是一个除法表达式,有时可能以零作为分母。如果是这种情况,我需要一种方法来逃避,以便它只返回 0。 我
我正在学习核心数据,特别是聚合。 当前我想要做的事情:计算表中在某些条件上具有逆关系的多对关系的记录数。 目前我正在这样做: NSExpression *ex = [NSExpression expr
我需要有关 Delphi 中的 ClientDatasets 的一些帮助。 我想要实现的是一个显示客户的网格,其中一列显示每个客户的订单数量。我将 ClientDataset 放在表单上并从 Delp
我的集合有 10M 个文档,并且有一个名为 movieId 的字段;该文档具有以下结构: { "_id" : ObjectId("589bed43e3d78e89bfd9b779"), "us
这个问题已经有答案了: What is the difference between association, aggregation and composition? (21 个回答) 已关闭 9
我在 elasticsearch 中有一些类似于这些示例的文档: { "id": ">", "list": [ "a", "b", "c" ] } { "id"
我正在做一些聚合。但是结果完全不是我所期望的,似乎它们没有聚合索引中与我的查询匹配的所有文档,在这种情况下 - 它有什么好处? 例如,首先我做这个查询: {"index":"datalayer","t
假设我在 ES 中有这些数据。 | KEY | value | |:-----------|------------:| | A |
可能在我的文档中,我有一个被分析的文本字段。我只是在ElasticSearch AggregationAPI中迷路了。我需要2种不同情况的支持: 情况A)结果是带有计数标记(条款)的篮子下降。 情况B
我正在为网上商店构建多面过滤功能,如下所示: Filter on Brand: [ ] LG (10) [ ] Apple (5) [ ] HTC (3) Filter on OS: [ ] Andr
我有一个父/子关系并且正在搜索 child 。 是否可以在父属性上创建聚合? 例如parent 是 POST,children 是 COMMENT。如果父项具有“类别”属性,是否可以搜索 COMMEN
我是一名优秀的程序员,十分优秀!