You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何筛选含边界框坐标列表的行并将坐标存入新列?

解决方法

针对你的需求,我们可以通过两种方式处理:使用tidyverse工具链(更简洁)或基础R代码。

前提说明

你的geo列应该是每行对应一个Feature嵌套列表(原dput输出的格式存在小问题,已修正为每行一个Feature结构)。


方法一:用tidyverse包(推荐)

先加载dplyr和purrr,然后分两步完成过滤和坐标提取:

library(dplyr)
library(purrr)

# 修正数据的geo列结构(确保每行对应一个Feature对象)
df$geo <- list(
  list(type = "Feature", bbox = list(8.2773, 48.9405, 8.5418, 49.0914), properties = structure(list(), .Names = character(0))),
  list(type = "Feature", bbox = list(8.2773, 48.9405, 8.5418, 49.0914), properties = structure(list(), .Names = character(0))),
  list(type = "Feature", bbox = list(8.2773, 48.9405, 8.5418, 49.0914), properties = structure(list(), .Names = character(0))),
  list(type = "Feature", bbox = list(9.8986, 53.5871, 9.8986, 53.5871), properties = structure(list(), .Names = character(0))),
  list(type = "Feature", bbox = list(9.8986, 53.5871, 9.8986, 53.5871), properties = structure(list(), .Names = character(0))),
  list(type = "Feature", bbox = list(9.8986, 53.5871, 9.8986, 53.5871), properties = structure(list(), .Names = character(0)))
)

# 1. 过滤出包含bbox的行
filtered_df <- df %>%
  filter(map_lgl(geo, ~ "bbox" %in% names(.x)))

# 2. 提取bbox坐标到单独列(顺序:min_lon, min_lat, max_lon, max_lat)
result_df <- filtered_df %>%
  mutate(
    min_lon = map_dbl(geo, ~ .x$bbox[[1]]),
    min_lat = map_dbl(geo, ~ .x$bbox[[2]]),
    max_lon = map_dbl(geo, ~ .x$bbox[[3]]),
    max_lat = map_dbl(geo, ~ .x$bbox[[4]])
  ) %>%
  select(-geo)  # 可选:移除原geo列

方法二:基础R实现

如果不想依赖第三方包,用基础R代码同样可以完成:

# 1. 筛选包含bbox的行
keep_rows <- sapply(df$geo, function(x) "bbox" %in% names(x))
filtered_df <- df[keep_rows, ]

# 2. 提取所有bbox坐标并转成数据框
bbox_df <- as.data.frame(do.call(rbind, lapply(filtered_df$geo, function(x) unlist(x$bbox))))
colnames(bbox_df) <- c("min_lon", "min_lat", "max_lon", "max_lat")

# 3. 合并坐标列到原数据
result_df <- cbind(filtered_df, bbox_df)
result_df$geo <- NULL  # 可选:移除原geo列

处理完成后,result_df就是只保留含有效边界框的行,且四个坐标分别存在单独列中的数据框。

内容的提问来源于stack exchange,提问作者Daniel AG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 04:40:41