Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从R中的索引命名嵌套列表的元素_R_List_Nested - Fatal编程技术网

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

从R中的索引命名嵌套列表的元素,r,list,nested,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", "dimen

我得到了这个嵌套列表:

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"
等等


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

我们可以使用
melt
(从
restrape2
)将嵌套的
列表转换为一个data.frame,带有索引列('L1','L2','L3')和'value'列,将其转换为
数据.table
setDT(…)
),按'L1','L2','L3'分组,我们获得行序列(
1:.N
),
do粘贴行中的元素。调用
单个
向量
,然后
通过指定
骨架
将其重新列出到与“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"
库(data.table)
图书馆(E2)

dico2使用双线体尝试此递归函数。它不假设固定的深度,并允许不平衡的列表。没有使用任何软件包

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

indexer <- function(L, lev = character(0)) {
    if (!is.list(L)) paste0(lev, seq_along(L))
    else Map(indexer, L, paste0(lev, seq_along(L), "|"))
}
示例2下面是一个深度不同且缺乏平衡的列表示例:

L <- list(list(1:3, 5:7), 9:10)
> 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"
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"