Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract contents from a nested list in R

Tags:

list

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

I would like extract a vector with all "fab_id" from a cliente given its name (In this case "LOLETE" or "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:

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. 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").

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?

Any good link to work with lists in R in this sense is welcomed.

Thanks in advance!

like image 313
alvaropr Avatar asked May 16 '18 09:05

alvaropr


People also ask

How do I extract elements from a nested list in R?

you can use pluck to extract or map extraction feature using map(l, "somestring") , or even create a function that extract what you want of a single loc list, then map that function on the long list. You'll have to deal with the returned type depending on how you want to organize your results at the end (list, data.

How do I extract items from a list in R?

The [[ operator can be used to extract single elements from a list. Here we extract the first element of the list. The [[ operator can also use named indices so that you don't have to remember the exact ordering of every element of the list. You can also use the $ operator to extract elements by name.

How do I select an element from a list in R?

The list() function is used to create lists in R programming. We can use the [[index]] function to select an element in a list.

How do I extract the first element of a list in R?

To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).


1 Answers

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"
like image 96
zx8754 Avatar answered Nov 08 '22 10:11

zx8754