我有几个记录了温度和盐度的地点的浮标数据.它位于空间数据帧中:

下面显示的数据都来自同一个浮标.

> head(CB_noaa_TS_2018_2021)
Simple feature collection with 6 features and 3 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -76.4432 ymin: 38.97071 xmax: -76.44319 ymax: 38.97074
CRS:           NA
# A tibble: 6 × 4
  date_time           Temperature Salinity             geometry
  <dttm>                    <dbl>    <dbl>              <POINT>
1 2018-07-01 01:00:00        26.9     7.28  (-76.4432 38.97073)
2 2018-07-01 02:00:00        26.8     7.29 (-76.44319 38.97074)
3 2018-07-01 03:00:00        26.8     7.31  (-76.4432 38.97074)
4 2018-07-01 04:00:00        26.9     7.37  (-76.4432 38.97073)
5 2018-07-01 05:00:00        27.4     7.34  (-76.4432 38.97071)
6 2018-07-01 09:00:00        26.7     7.32  (-76.4432 38.97074)

这个数据集中有几个相距很多公里的浮标;然而,由于位置随时间(米)发生轻微变化,坐标(来自GPS)波动,使其看起来像数百个不同的位置.我想把这些精确的位置组合成每个浮标的一般位置.

有没有一个空间等效于Lubridate的"floor_date",我可以在这里舍入坐标,或者用其他方法降低坐标的精度?

推荐答案

我想你要找的是st_centroid,它可以用来计算一组点的空间平均值.你可以这样使用它:

library(tidyverse)

CB_noaa_TS_2018_2021 %>%
  mutate(geometry = st_centroid(st_combine(geometry)))
#> Simple feature collection with 6 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -76.4432 ymin: 38.97073 xmax: -76.4432 ymax: 38.97073
#> CRS:           NA
#>             date_time Temperature Salinity                  geometry
#> 1 2018-07-01 01:00:00        26.9     7.28 POINT (-76.4432 38.97073)
#> 2 2018-07-01 02:00:00        26.8     7.29 POINT (-76.4432 38.97073)
#> 3 2018-07-01 03:00:00        26.8     7.31 POINT (-76.4432 38.97073)
#> 4 2018-07-01 04:00:00        26.9     7.37 POINT (-76.4432 38.97073)
#> 5 2018-07-01 05:00:00        27.4     7.34 POINT (-76.4432 38.97073)
#> 6 2018-07-01 09:00:00        26.7     7.32 POINT (-76.4432 38.97073)

通过以黑色绘制原始点,以红色绘制平均点,可以直观地看到这一点的作用:

CB_noaa_TS_2018_2021 %>%
  pluck(4) %>%
  plot()

CB_noaa_TS_2018_2021 %>%
  mutate(geometry = st_centroid(st_combine(geometry))) %>%
  pluck(4) %>%
  plot(col = "red", add = TRUE)

enter image description here


Reproducible version of data

library(sf)

CB_noaa_TS_2018_2021 <- st_sf(structure(list(date_time = structure(
  c(1530406800, 1530410400, 
1530414000, 1530417600, 1530421200, 1530435600), class = c("POSIXct", 
"POSIXt"), tzone = ""), Temperature = c(26.9, 26.8, 26.8, 26.9, 
27.4, 26.7), Salinity = c(7.28, 7.29, 7.31, 7.37, 7.34, 7.32), 
    geometry = structure(list(structure(c(-76.4432, 38.97073), class = c("XY", 
    "POINT", "sfg")), structure(c(-76.44319, 38.97074), class = c("XY", 
    "POINT", "sfg")), structure(c(-76.4432, 38.97074), class = c("XY", 
    "POINT", "sfg")), structure(c(-76.4432, 38.97073), class = c("XY", 
    "POINT", "sfg")), structure(c(-76.4432, 38.97071), class = c("XY", 
    "POINT", "sfg")), structure(c(-76.4432, 38.97074), class = c("XY", 
    "POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0,
    bbox = structure(c(xmin = -76.4432, ymin = 38.97071, xmax = -76.44319, 
    ymax = 38.97074), class = "bbox"), crs = structure(list(input = 
    NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L)), 
    row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame"))

CB_noaa_TS_2018_2021
#> Simple feature collection with 6 features and 3 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -76.4432 ymin: 38.97071 xmax: -76.44319 ymax: 38.97074
#> CRS:           NA
#>             date_time Temperature Salinity                   geometry
#> 1 2018-07-01 01:00:00        26.9     7.28  POINT (-76.4432 38.97073)
#> 2 2018-07-01 02:00:00        26.8     7.29 POINT (-76.44319 38.97074)
#> 3 2018-07-01 03:00:00        26.8     7.31  POINT (-76.4432 38.97074)
#> 4 2018-07-01 04:00:00        26.9     7.37  POINT (-76.4432 38.97073)
#> 5 2018-07-01 05:00:00        27.4     7.34  POINT (-76.4432 38.97071)
#> 6 2018-07-01 09:00:00        26.7     7.32  POINT (-76.4432 38.97074)

reprex package(v2.0.1)于2022-05-09创建

R相关问答推荐

计算R中的威布尔分布的EDF

ggplot geom_smooth()用于线性回归虚拟变量-没有回归线

如何使用`ggplot2::geom_segment()`或`ggspatial::geom_spatial_segment()`来处理不在格林威治中心的sf对象?

如何自定义Shapviz图?

任意列的欧几里得距离

Highcharter多次钻取不起作用,使用不同方法

基于多列将值链接到NA

我想在R中总结一个巨大的数据框架,使我只需要唯一的lat、lon、Date(Year)和Maxium Value""""""""

根据多个条件增加y轴高度以适应geom_text标签

如何得到R中唯一的组合群?

您是否可以折叠R中的重复行,同时保留基于所选列的值?

比较理论阿尔法和经验阿尔法

SHINY:使用JS函数应用的CSS样式显示HTML表格

如何从向量构造一系列双边公式

使用geom_iles在一个切片中包含多个值

将统计检验添加到GGPUBR中的盒图,在R

使用geom_sf跨越日期线时的闭合边界

如何更改包中函数中的参数?

R:使用ApexCharge更改标签在饼图中的位置

位置_道奇在geom_point图中不躲避