我有这个嵌套列表:
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"
Run Code Online (Sandbox Code Playgroud)
我想要的是创建一个具有相同结构的列表,但不是拥有原始值,我希望有一种"索引"名称,如:
[[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"
Run Code Online (Sandbox Code Playgroud)
等等......
当然,通过不同的嵌套索引,元素的数量不是恒定的.谁知道怎么做?我听说过rapply,但我无法做到.
尝试使用2行机身的这种递归功能.它不假定固定深度并允许不平衡列表.没有使用包裹.
它接受一个对象L和一个级别.如果对象不是列表,那么我们有一个叶子,它返回它的级别.如果对象是一个列表,那么我们有一个节点,它迭代它的组件indexer,每次通过串联lev,i并|为第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), "|"))
}
Run Code Online (Sandbox Code Playgroud)
示例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"
Run Code Online (Sandbox Code Playgroud)
示例2以下是具有不同深度和不平衡的列表的示例:
L <- list(list(1:3, 5:7), 9:10)
Run Code Online (Sandbox Code Playgroud)
赠送:
> 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"
Run Code Online (Sandbox Code Playgroud)