[英]Extract contents from a nested list in R
I have my clients data stored in a nested list in R, in the same way than this one: 我将客户数据存储在R中的嵌套列表中,其方式与此相同:
myinventedlist <- list("LOLETE" = list("Name" = "LOLETE",
"location" = "Huelva",
"Employees" = "22",
"SM" = "eJeK1",
"Groups" = list("ABUELOs" = list("PICHI" = list("fab_name" = "Pichi (ES)", "fab_id" = "2323423ES", "fab_tarif" = "6A"),
"PACHA" = list("fab_name" = "Pacha (AG)", "fab_id" = "1231212AG", "fab_tarif" = "6A"),
"POCHO" = list("fab_name" = "Pocho (ED)", "fab_id" = "2132192ED", "fab_tarif" = "6A")),
"PRIMOts" = list("MONGO" = list("fab_name" = "MONGO (LB)", "fab_id" = "21332238LB", "fab_tarif" = "6A"),
"MINGO" = list("fab_name" = "MINGO (NT)", "fab_id" = "22231220NT", "fab_tarif" = "6B"),
"MUNGO" = list("fab_name" = "MUNGO (CQ)", "fab_id" = "23215001CQ", "fab_tarif" = "6B")))),
"GUPERA" = list("Name" = "GUPERA",
"location" = "Madrid",
"Employees" = "113",
"SM" = "1xa3P",
"Groups" = list("ABUELOs" = list("YYTER" = list("fab_name" = "YYTER (MM)", "fab_id" = "2323423MM", "fab_tarif" = "6A"),
"LOLE" = list("fab_name" = "LOLE (NN)", "fab_id" = "1231212NN", "fab_tarif" = "6A"),
"PEEE" = list("fab_name" = "PEE (EE)", "fab_id" = "2132192EE", "fab_tarif" = "6A")))))
I would like extract a vector with all "fab_id" from a cliente given its name (In this case "LOLETE" or "GUPERA"). 我想从给定名称的cliente中提取一个带有所有“ fab_id”的向量(在本例中为“ LOLETE”或“ GUPERA”)。
I can access the desired content, that is, all "fab_id" from a certain Client, but it is a horrible way to do so: 我可以从某个客户端访问所需的内容,即所有“ fab_id”,但这是一种可怕的方式:
cliente <- "LOLETE"
firstindex <- which(names(myinventedlist) == eval(cliente))
secondindex <- which(names(myinventedlist[[firstindex]]) == "Groups")
sapply(myinventedlist[[firstindex]][[secondindex]][[1]], "[[", "fab_id")
sapply(myinventedlist[[firstindex]][[secondindex]][[2]], "[[", "fab_id")
Which gives: 这使:
PICHI PACHA POCHO
"2323423ES" "1231212AG" "2132192ED"
MONGO MINGO MUNGO
"21332238LB" "22231220NT" "23215001CQ
I would like that given the client I could recover all the "fab_id" disregarding the "Group" they belong to. 我希望给定客户我可以恢复所有“ fab_id”,而不必考虑它们所属的“组”。 The client is passed as a String.
客户端作为字符串传递。
In another words, I would like to be able to obtain all the elements values that are labelled under certain a title (like "fab_name") within a list, altough they might be included in nested lists (like "Groups"). 换句话说,我希望能够获得列表中某个标题(例如“ fab_name”)下标记的所有元素值,尽管它们可能会包含在嵌套列表(例如“ Groups”)中。
I would like to take adventage and ask if in this kind of list for storing data that will be used recursevely in a project it is good to name the clients as "CLIENT01" and then add the field "clien_name" within the list or if it is ok to name the list directly with the name of the client. 我想尝试一下,问是否在此类列表中存储将在项目中递归使用的数据,将客户端命名为“ CLIENT01”,然后在列表中添加字段“ clien_name”是好事,还是可以直接使用客户端名称来命名列表。 What is the typical way to go?
典型的做法是什么?
Any good link to work with lists in R in this sense is welcomed. 欢迎使用这种方式在R中使用列表的任何良好链接。
Thanks in advance! 提前致谢!
unlist
, then subset by names grepl
: unlist
,然后按名称grepl
命名子集:
res <- unlist(myinventedlist[[ cliente ]])
res[ grepl("fab_id", names(res)) ]
# Groups.ABUELOs.PICHI.fab_id Groups.ABUELOs.PACHA.fab_id Groups.ABUELOs.POCHO.fab_id Groups.PRIMOts.MONGO.fab_id
# "2323423ES" "1231212AG" "2132192ED" "21332238LB"
# Groups.PRIMOts.MINGO.fab_id Groups.PRIMOts.MUNGO.fab_id
# "22231220NT" "23215001CQ"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.