使用包含 lubridate 间隔的列表列取消嵌套 sf 数据框
Unnesting sf dataframe with list-column containing lubridate intervals
我从一个名为 polygon_history
的 sf 数据框开始,其中包含以下列:
polygon_id,valid_period,geometry
其中 valid_period
是使用 lubridate 包创建的两个日期之间的间隔。
我将 valid_period
细分为更小的间隔,并通过获取 valid_period
与我感兴趣的日期范围列表 (sectionized_periods
) 的交集将它们组合在一个列表中单独分析,代码如下:
sectionized_periods
[1] 2021-09-01 UTC--2021-09-07 UTC 2021-09-07 UTC--2021-10-10 UTC 2021-10-10 UTC--2021-10-24 UTC 2021-10-24 UTC--2021-11-30 UTC
polygon_history %>%
rowwise %>%
mutate(period_section = list(c(lubridate::intersect(valid_period,sectionized_periods))))
现在我的 table 有以下列:
polygon_id, valid_period, period_sections, geometry
其中 period_sections
是一个列表列;每行都是 [lubridate] 间隔的列表。
period_sections
中的示例行:
polygon_history$period_section[1]
[[1]]
[1] 2021-09-01 UTC--2021-09-07 UTC 2021-09-07 UTC--2021-10-10 UTC 2021-10-10 UTC--2021-10-24 UTC 2021-10-24 UTC--2021-11-30 UTC
我正在尝试根据 period_section
列表列取消嵌套此 table,以便为列表的每个元素(每个间隔)创建一行,但是当我 运行 下面的代码,我只得到 period_section
中的数值而不是间隔。我相信这个数值代表间隔的持续时间,但我需要有间隔。
polygon_history <- polygon_history %>%
+ rowwise %>%
+ mutate(PERIOD_SECTION = list(c(lubridate::intersect(VALID_PERIOD,sectionized_periods)))) %>%
+ unnest(PERIOD_SECTION) %>%
+ sf::st_sf()
polygon_history$PERIOD_SECTION
An object of class "vctrs:::common_class_fallback"
[1] 518400 2851200 1209600 3196800 518400 2851200 1209600 3196800 518400 2851200 1209600 3196800 518400 2851200 1209600 3196800 518400 2851200
[19] 1209600
感谢任何有关如何解决此问题的见解。谢谢!
我能够使用 unchop
.
完成我需要的事情
polygon_history <- polygon_history %>%
+ rowwise %>%
+ mutate(PERIOD_SECTION = list(c(lubridate::intersect(VALID_PERIOD,sectionized_periods)))) %>%
+ unchop(PERIOD_SECTION) %>%
+ sf::st_sf()
我从一个名为 polygon_history
的 sf 数据框开始,其中包含以下列:
polygon_id,valid_period,geometry
其中 valid_period
是使用 lubridate 包创建的两个日期之间的间隔。
我将 valid_period
细分为更小的间隔,并通过获取 valid_period
与我感兴趣的日期范围列表 (sectionized_periods
) 的交集将它们组合在一个列表中单独分析,代码如下:
sectionized_periods
[1] 2021-09-01 UTC--2021-09-07 UTC 2021-09-07 UTC--2021-10-10 UTC 2021-10-10 UTC--2021-10-24 UTC 2021-10-24 UTC--2021-11-30 UTC
polygon_history %>%
rowwise %>%
mutate(period_section = list(c(lubridate::intersect(valid_period,sectionized_periods))))
现在我的 table 有以下列:
polygon_id, valid_period, period_sections, geometry
其中 period_sections
是一个列表列;每行都是 [lubridate] 间隔的列表。
period_sections
中的示例行:
polygon_history$period_section[1]
[[1]]
[1] 2021-09-01 UTC--2021-09-07 UTC 2021-09-07 UTC--2021-10-10 UTC 2021-10-10 UTC--2021-10-24 UTC 2021-10-24 UTC--2021-11-30 UTC
我正在尝试根据 period_section
列表列取消嵌套此 table,以便为列表的每个元素(每个间隔)创建一行,但是当我 运行 下面的代码,我只得到 period_section
中的数值而不是间隔。我相信这个数值代表间隔的持续时间,但我需要有间隔。
polygon_history <- polygon_history %>%
+ rowwise %>%
+ mutate(PERIOD_SECTION = list(c(lubridate::intersect(VALID_PERIOD,sectionized_periods)))) %>%
+ unnest(PERIOD_SECTION) %>%
+ sf::st_sf()
polygon_history$PERIOD_SECTION
An object of class "vctrs:::common_class_fallback"
[1] 518400 2851200 1209600 3196800 518400 2851200 1209600 3196800 518400 2851200 1209600 3196800 518400 2851200 1209600 3196800 518400 2851200
[19] 1209600
感谢任何有关如何解决此问题的见解。谢谢!
我能够使用 unchop
.
polygon_history <- polygon_history %>%
+ rowwise %>%
+ mutate(PERIOD_SECTION = list(c(lubridate::intersect(VALID_PERIOD,sectionized_periods)))) %>%
+ unchop(PERIOD_SECTION) %>%
+ sf::st_sf()