簡體   English   中英

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

[英]Naming elements of a nested list from their index in R

我有這個嵌套列表:

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"

我們可以使用melt (來自reshape2 )將嵌套list轉換為帶有索引列('L1','L2','L3')和'value'列的data.table ,將其轉換為data.tablesetDT(...) ),按'L1','L2','L3'分組,我們得到行的序列( 1:.N ),用do.call將行的元素paste到單個vector ,然后relist到一個list通過指定與所述結構相同的結構“迪科”的skeleton

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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM