在R中解析DataFrame的numpy_array列嵌套数组结构
处理包含嵌套numpy数组字符串的DataFrame
我们可以用tidyverse工具链完成需求,以下是完整解决方案:
1. 加载依赖包与数据
library(tidyverse) # 导入你的数据 df <- structure(list(ID = 101:103, Binary = c(0L, 0L, 0L), Continuous = c(0L, 2L, 5L), date1 = c("20-01-01", "21-01-01", "22-01-01"), date2 = c("21-01-01", "22-01-01", "23-01-01"), character = c("CHAR", "CHAR", "CHAR" ), numpy_array = c("[0.012110, 0.11211022, 0.110101, 0.102020102, 0.111, 0.10, 0.11, 0.10220, 0.011202220, 0.120220, 0.211011], [0.10201011202, 0.220, 0.02010]", "[0.11012, 0.11303013202, 0.1012012010213, 0.303320301, 0.1312, 0.0203021], [0.02310, 0.111, 0.2023, 0.01120, 0.03201120, 0.101211202, 0.012220, 0.1130301030, 0.013], [0.22123020, 0.21010312, 0.013032020, 0.20301303233, 0.3010231323, 0.033303, 0.131021022020]", "[0.0232030302, 0.22, 0.0210, 0.32101011013, 0.2011230, 0.301110, 0.10, 0.012133, 0.202301, 0.10222210, 0.103221, 0.102120220, 0.21030, 0.31030102, 0.123], [0.1032230, 0.0323101, 0.10132120, 0.102033, 0.312010, 0.11310221201, 0.2, 0.3302, 0.311, 0.231203020, 0.01312123, 0.2303210301, 0.111230320, 0.31220132, 0.023101312202, 0.133023], [0.310330, 0.20301020, 0.210303, 0.213011030, 0.0302030332, 0.1322222, 0.1202302301, 0.33220220, 0.21332, 0.12103030, 1.301, 0.1232033], [0.01010303012, 0.3010330220, 0.10303230, 0.11030, 0.130102, 0.313121, 0.2113101020, 0.121010, 0.0320221033, 0.0303303, 0.123301, 0.3013021, 0.31031022102, 0.21013112021, 0.0102101]" )), class = "data.frame", row.names = c(NA, -3L))
2. 统计子数组数量与元素数
对numpy_array列拆分提取子数组,统计总数和每个子数组的元素数:
df_processed <- df %>% # 提取所有[]包裹的子数组,存入list列 mutate(subarrays = str_extract_all(numpy_array, "\\[.*?\\]")) %>% # 统计每行的子数组总数 mutate(subarray_count = map_int(subarrays, length)) %>% # 统计每个子数组的元素数量 mutate(subarray_element_counts = map(subarrays, function(sa) { sa %>% str_remove_all("\\[|\\]") %>% str_split(",\\s*") %>% map_int(length) }))
执行后:
subarray_count列存储每行的子数组总数subarray_element_counts列存储每个子数组对应的元素数列表
3. 拆分元素为结构化列
将子数组元素拆分为Cell_1_Value_1、Cell_1_Value_2格式的列:
df_wide <- df_processed %>% # 把list格式的子数组拆分为多列 unnest_wider(subarrays, names_sep = "_") %>% # 转换为长格式处理每个子数组 pivot_longer(cols = starts_with("subarrays_"), names_to = "cell_id", values_to = "elements") %>% drop_na(elements) %>% # 重命名cell编号为Cell_1、Cell_2格式 mutate(cell_id = str_replace(cell_id, "subarrays_(\\d+)", "Cell_\\1")) %>% # 拆分每个子数组的元素为单独行 mutate(elements = str_remove_all(elements, "\\[|\\]") %>% str_split(",\\s*")) %>% unnest_longer(elements, indices_to = "value_id") %>% mutate(value_id = paste0("_Value_", value_id)) %>% # 组合成最终列名 unite(col_name, cell_id, value_id, sep = "") %>% # 转回宽格式 pivot_wider(names_from = col_name, values_from = elements, values_fill = NA) %>% # 合并统计列与原数据列 left_join(df_processed %>% select(ID, subarray_count, subarray_element_counts), by = "ID") %>% left_join(df %>% select(-numpy_array), by = "ID")
最终的df_wide包含:
- 原数据的所有基础列(
ID、Binary等) - 子数组统计列(
subarray_count、subarray_element_counts) - 所有拆分后的结构化元素列(
Cell_1_Value_1等)
内容的提问来源于stack exchange,提问作者TDeramus
相关产品推荐
相关产品推荐

