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

时间:2016-09-10 13:51:54

标签: r list nested

我有这个嵌套列表:

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 个答案:

答案 0 :(得分:7)

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

它接受一个对象L和一个级别。如果对象不是列表,那么我们有一个叶子,它返回它的级别。如果对象是一个列表,那么我们有一个节点,它会遍历其组件,在每个组件上调用indexerlevi连接|的第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"

答案 1 :(得分:3)

我们可以使用reshape2(来自list)将嵌套的data.table转换为带有索引列的data.frame(&#39; L1&#39;,&#39 ; L2&#39;,&#39; L3&#39;)和&#39;值&#39;列,将其转换为setDT(...)1:.N),按&#39; L1&#39;,&#39; L2&#39;&#39; L3&#39;分组,我们得到行的序列(paste),do.callvectorrelist的行的元素,然后是listskeleton具有与“dico”相同的结构。通过指定library(data.table) library(reshape2) dico2 <- relist(do.call(paste, c(setDT(melt(dico))[, 1:.N , by = .(L1, L2, L3)], sep="|")), skeleton = dico) dico2 #[[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" #... #[[3]][[3]] #[[3]][[3]][[1]] #[1] "3|3|1|1" "3|3|1|2" "3|3|1|3" #[[3]][[3]][[2]] #[1] "3|3|2|1" "3|3|2|2" "3|3|2|3"

document.getElementById(ev.target.id).innerHTML = y + "<a href=" + linkurl + " > + linktext + </a>";