获取R中列表列表元素的长度 [英] get the lengths of element of lists of list in R

查看:713
本文介绍了获取R中列表列表元素的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的叫dico的列表:

Here is my list called dico :

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]][[2]]
[[1]][[2]][[1]]
[1] "tamano"    "volumen"   "dimension" "talla"    

[[1]][[2]][[2]]
[1] "45 cm"          "45"             "0.45 M"         "45 centimiento"


[[1]][[3]]
[[1]][[3]][[1]]
[1] "measures"    "dimension"   "measurement"

[[1]][[3]][[2]]
[1] "45 cm"      "0.45 m"     "100 inches" "100 pouces"



[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
[1] "poids"     "poid"      "poids net"

[[2]][[1]][[2]]
[1] "100 grammes" "100 gr"      "100"        


[[2]][[2]]
[[2]][[2]][[1]]
[1] "peso"            "carga"           "peso especifico"

[[2]][[2]][[2]]
[1] "100 gramos" "100g"       "100"        "100 g"     


[[2]][[3]]
[[2]][[3]][[1]]
[1] "weight"           "net wieght"       "weight (grammes)"

[[2]][[3]][[2]]
[1] "100 grams" "100"       "100 g" 
...

我想获取列表中所有元素的所有长度.所以我想要一个看起来像这样的输出:

I want to get all the lengths of all the elements of my list. So I want an output which would look like this :

[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[1] 3   

[[1]][[1]][[2]]
[1] 4


[[1]][[2]]
[[1]][[2]][[1]]
[1] 4   

[[1]][[2]][[2]]
[1] 4


[[1]][[3]]
[[1]][[3]][[1]]
[1] 3

[[1]][[3]][[2]]
[1] 4



[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
[1] 3

[[2]][[1]][[2]]
[1] 3      


[[2]][[2]]
[[2]][[2]][[1]]
[1] 3

[[2]][[2]][[2]]
[1] 4  


[[2]][[3]]
[[2]][[3]][[1]]
[1] 3

[[2]][[3]][[2]]
[1] 3
...

我知道我应该使用lapply,sapply和东西,但是我被困了几个小时.你会怎么做?

I know I should use lapply, sapply and stuff but I got stuck for hours. How would you do it?

推荐答案

我们可以使用lapply的递归版本,即rapply

We can use the recursive version of lapply i.e. rapply

rl <- rapply(dico, length, how="list")
rl
#[[1]]
#[[1]][[1]]
#[[1]][[1]][[1]]
#[1] 4

#[[1]][[1]][[2]]
#[1] 4


#[[1]][[2]]
#[[1]][[2]][[1]]
#[1] 4

#[[1]][[2]][[2]]
#[1] 4
#---

这篇关于获取R中列表列表元素的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆