【发布时间】:2020-08-24 23:18:41
【问题描述】:
我有一个需要整理的具有复杂嵌套结构的列表(从 API JSON 导入)。我只设法用 for 循环来做到这一点,这似乎不是最优的:写起来很长,用许多元素计算很长,如果出现新的变化可能会出错。有没有办法使用 tidyverse 函数(例如 map()、unnnest()、flatten()、squash() 或基本 R 中的等效函数)获得类似的结果?我在 StackOverflow 中没有找到任何具有类似问题组合的示例(在示例下方指出)。
这是可重现的示例:
metadata <- list(
table1 = list(
attribute1 = "tb1_att1",
level_to_disard = list(
attribute2 = "tb1_att2",
columns = list(
column1 = list(
col_name = "tb1_col1_name",
col_type = "tb1_col1_type"
),
column2 = list(
col_name = "tb1_col2_name",
col_type = "tb1_col2_type"
)
),
tags = c("tag1", "tag2", "tag3"),
irrelevant = list(irrelveant1 = "blabla", irrelevant2 = "blibli")
)
),
table2 = list(
attribute1 = "tb2_att1",
level_to_disard = list(
columns = list(
column1 = list(
col_name = "tb2_col1_name",
col_irrelevant = "bloblo"
),
column2 = list(
col_name = "tb2_col2_name",
col_type = "tb2_col2_type"
)
),
tags = c("tag1", "tag3")
)
)
)
str(metadata)
# Output in console:
List of 2
$ table1:List of 2
..$ attribute1 : chr "tb1_att1"
..$ level_to_disard:List of 4
.. ..$ attribute2: chr "tb1_att2"
.. ..$ columns :List of 2
.. .. ..$ column1:List of 2
.. .. .. ..$ col_name: chr "tb1_col1_name"
.. .. .. ..$ col_type: chr "tb1_col1_type"
.. .. ..$ column2:List of 2
.. .. .. ..$ col_name: chr "tb1_col2_name"
.. .. .. ..$ col_type: chr "tb1_col1_type"
.. ..$ tags : chr [1:3] "tag1" "tag2" "tag3"
.. ..$ irrelevant:List of 2
.. .. ..$ irrelveant1: chr "blabla"
.. .. ..$ irrelevant2: chr "blibli"
$ table2:List of 2
..$ attribute1 : chr "tb2_att1"
..$ level_to_disard:List of 2
.. ..$ columns:List of 2
.. .. ..$ column1:List of 2
.. .. .. ..$ col_name : chr "tb2_col1_name"
.. .. .. ..$ col_irrelevant: chr "bloblo"
.. .. ..$ column2:List of 2
.. .. .. ..$ col_name: chr "tb2_col2_name"
.. .. .. ..$ col_type: chr "tb2_col1_type"
.. ..$ tags : chr [1:2] "tag1" "tag3"
请注意,属性位于不同的级别,必须丢弃某些元素(列表中名为“不相关”),表 2 中缺少“attribute2”,表 2 的第 1 列中缺少“类型”。 这是 for 循环的解决方案和预期的结果。
# Define a function to extract column information
extract_cols <- function(x){
fields <- tibble()
if (length(x) == 0) {
return(fields)
} else {
for (i in 1:length(x)) {
fields <- add_row(fields)
# Extract name
fields$name[i] = ""
# Extract type if present of return empty string
if (any(names(x[[i]]) == "type")) {
fields$type[i] = x[[i]][["type"]]
} else {
fields$type[i] = ""
}
return(fields)
}
}
}
# Create an empty tibble for the tidy metadata. It could also be a list.
library(tibble)
meta <- tibble()
# for (i in 1:1) {
for (i in 1:length(metadata)) {
meta <- add_row(meta)
meta$attribute1[[i]] <- metadata[[i]][["attribute1"]]
meta$attribute2[[i]] <- ifelse(length(metadata[[i]][["level_to_disard"]][["attribute2"]]) > 0,
c(metadata[[i]][["level_to_disard"]][["attribute2"]]), "")
metadata[[i]][["level_to_disard"]][["attribute2"]]
meta$cols[[i]] <- extract_cols(metadata[[i]][["columns"]])
meta$tags[[i]] <- metadata[[i]][["level_to_disard"]][["tags"]]
}
str(meta)
# Output in console:
tibble [2 × 4] (S3: tbl_df/tbl/data.frame)
$ attribute1: chr [1:2] "tb1_att1" "tb2_att1"
$ attribute2: chr [1:2] "tb1_att2" ""
$ cols :List of 2
..$ : tibble [0 × 0] (S3: tbl_df/tbl/data.frame)
Named list()
..$ : tibble [0 × 0] (S3: tbl_df/tbl/data.frame)
Named list()
$ tags :List of 2
..$ : chr [1:3] "tag1" "tag2" "tag3"
..$ : chr [1:2] "tag1" "tag3"
有没有更直接的方法来获得这个结果?输出可以是列表、tibble 或数据框,只要使用与上述 'meta'enter code here 类似的结构进行简化即可。
【问题讨论】: