gpt4 book ai didi

r - 从 R 中的索引命名嵌套列表的元素

转载 作者:行者123 更新时间:2023-12-02 11:08:46 25 4
gpt4 key购买 nike

我得到了这个嵌套列表:

dico <- list(list(list(c("dim.", "dimension", "dimensions", "mesures"
), c("45 cm", "45", "45 CM", "0.45m")), list(c("tamano", "volumen",
"dimension", "talla"), c("45 cm", "45", "0.45 M", "45 centimiento"
)), list(c("measures", "dimension", "measurement"), c("45 cm",
"0.45 m", "100 inches", "100 pouces"))), list(list(c("poids",
"poid", "poids net"), c("100 grammes", "100 gr", "100")), list(
c("peso", "carga", "peso especifico"), c("100 gramos", "100g",
"100", "100 g")), list(c("weight", "net wieght", "weight (grammes)"
), c("100 grams", "100", "100 g"))), list(list(c("Batterie oui/non",
"batterie", "présence batterie"), c("Oui", "batterie", "OUI"
)), list(c("bateria", "bateria si or no", "bateria disponible"
), c("si", "bateria furnindo", "1")), list(c("Battery available",
"battery", "battery yes or no"), c("yes", "Y", "Battery given"
))))

[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[1] "dim." "dimension" "dimensions" "mesures"

[[1]][[1]][[2]]
[1] "45 cm" "45" "45 CM" "0.45m"

我想要的是创建一个具有相同结构的列表,但我想要一种“索引”名称,而不是原始值,例如:

[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[1] "1|1|1|1" "1|1|1|2" "1|1|1|3" "1|1|1|4"

[[1]][[1]][[2]]
[1] "1|1|2|1" "1|1|2|2" "1|1|2|3" "1|1|2|4"

等等...

当然,通过不同的嵌套索引,元素的数量并不是恒定的。有人知道该怎么做吗?我听说过 rapply,但我没能做到。

最佳答案

尝试使用 2 行主体的递归函数。它不假设固定深度并允许不平衡列表。没有使用任何包。

它接受一个对象L和一个关卡。如果对象不是列表,那么我们有一个叶子并返回其级别。如果对象是一个列表,那么我们有一个节点,它会迭代其组件,在每次传递 levi 和的串联时调用 indexer | 表示第 i 个组件的级别。

indexer <- function(L, lev = character(0)) {
if (!is.list(L)) paste0(lev, seq_along(L))
else Map(indexer, L, paste0(lev, seq_along(L), "|"))
}

示例 1 使用问题中的 dico

> str( indexer(dico) )
List of 3
$ :List of 3
..$ :List of 2
.. ..$ : chr [1:4] "1|1|1|1" "1|1|1|2" "1|1|1|3" "1|1|1|4"
.. ..$ : chr [1:4] "1|1|2|1" "1|1|2|2" "1|1|2|3" "1|1|2|4"
..$ :List of 2
.. ..$ : chr [1:4] "1|2|1|1" "1|2|1|2" "1|2|1|3" "1|2|1|4"
.. ..$ : chr [1:4] "1|2|2|1" "1|2|2|2" "1|2|2|3" "1|2|2|4"
..$ :List of 2
.. ..$ : chr [1:3] "1|3|1|1" "1|3|1|2" "1|3|1|3"
.. ..$ : chr [1:4] "1|3|2|1" "1|3|2|2" "1|3|2|3" "1|3|2|4"
$ :List of 3
..$ :List of 2
.. ..$ : chr [1:3] "2|1|1|1" "2|1|1|2" "2|1|1|3"
.. ..$ : chr [1:3] "2|1|2|1" "2|1|2|2" "2|1|2|3"
..$ :List of 2
.. ..$ : chr [1:3] "2|2|1|1" "2|2|1|2" "2|2|1|3"
.. ..$ : chr [1:4] "2|2|2|1" "2|2|2|2" "2|2|2|3" "2|2|2|4"
..$ :List of 2
.. ..$ : chr [1:3] "2|3|1|1" "2|3|1|2" "2|3|1|3"
.. ..$ : chr [1:3] "2|3|2|1" "2|3|2|2" "2|3|2|3"
$ :List of 3
..$ :List of 2
.. ..$ : chr [1:3] "3|1|1|1" "3|1|1|2" "3|1|1|3"
.. ..$ : chr [1:3] "3|1|2|1" "3|1|2|2" "3|1|2|3"
..$ :List of 2
.. ..$ : chr [1:3] "3|2|1|1" "3|2|1|2" "3|2|1|3"
.. ..$ : chr [1:3] "3|2|2|1" "3|2|2|2" "3|2|2|3"
..$ :List of 2
.. ..$ : chr [1:3] "3|3|1|1" "3|3|1|2" "3|3|1|3"
.. ..$ : chr [1:3] "3|3|2|1" "3|3|2|2" "3|3|2|3"

示例 2 下面是一个具有不同深度且缺乏平衡的列表示例:

L <- list(list(1:3, 5:7), 9:10)

给予:

> str( indexer(L) )
List of 2
$ :List of 2
..$ : chr [1:3] "1|1|1" "1|1|2" "1|1|3"
..$ : chr [1:3] "1|2|1" "1|2|2" "1|2|3"
$ : chr [1:2] "2|1" "2|2"

关于r - 从 R 中的索引命名嵌套列表的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39426743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com