问题描述
我将客户数据存储在R中的嵌套列表中,其方式与此相同:
I have my clients data stored in a nested list in R, in the same way than this one:
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")))))
我想从给定名称的客户端提取带有所有"fab_id"的向量(在本例中为"LOLETE"或"GUPERA").
I would like extract a vector with all "fab_id" from a cliente given its name (In this case "LOLETE" or "GUPERA").
我可以从某个客户端访问所需的内容,即所有"fab_id",但这是一种可怕的方式:
I can access the desired content, that is, all "fab_id" from a certain Client, but it is a horrible way to do so:
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")
哪个给:
PICHI PACHA POCHO
"2323423ES" "1231212AG" "2132192ED"
MONGO MINGO MUNGO
"21332238LB" "22231220NT" "23215001CQ
我希望给定客户我可以恢复所有"fab_id"而无视他们所属的组".客户端作为字符串传递.
I would like that given the client I could recover all the "fab_id" disregarding the "Group" they belong to. The client is passed as a String.
换句话说,我希望能够获得列表中某个标题(例如"fab_name")下标记的所有元素值,尽管它们可能会包含在嵌套列表(例如"Groups")中
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").
我想冒险一下,问一下在这种列表中是否存储将在项目中递归使用的数据,将客户端命名为"CLIENT01",然后在列表中添加字段"clien_name"是否很好?或者可以直接使用客户端名称来命名列表.典型的做法是什么?
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. What is the typical way to go?
欢迎使用这种方式在R中使用列表的任何良好链接.
Any good link to work with lists in R in this sense is welcomed.
提前谢谢!
推荐答案
unlist
,然后按名称grepl
的子集:
unlist
, then subset by names 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"
这篇关于从R中的嵌套列表中提取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!