How to access items in a complex list?

I have a nice list that looks like this:

tmp = NULL
t = NULL
tmp$resultitem$count = "1057230"
tmp$resultitem$status = "Ok"
tmp$resultitem$menu = "PubMed"
tmp$resultitem$dbname = "pubmed"
t$resultitem$count = "305215"
t$resultitem$status = "Ok"
t$resultitem$menu = "PMC"
t$resultitem$dbname = "pmc"
tmp = c(tmp, t)
t = NULL
t$resultitem$count = "1"
t$resultitem$status = "Ok"
t$resultitem$menu = "Journals"
t$resultitem$dbname = "journals"
tmp = c(tmp, t)

What produces:

> str(tmp)
List of 3
 $ resultitem:List of 4
  ..$ count : chr "1057230"
  ..$ status: chr "Ok"
  ..$ menu  : chr "PubMed"
  ..$ dbname: chr "pubmed"
 $ resultitem:List of 4
  ..$ count : chr "305215"
  ..$ status: chr "Ok"
  ..$ menu  : chr "PMC"
  ..$ dbname: chr "pmc"
 $ resultitem:List of 4
  ..$ count : chr "1"
  ..$ status: chr "Ok"
  ..$ menu  : chr "Journals"
  ..$ dbname: chr "journals"

Now I want to search for each item resultitem. I want to know dbnamefor every database that has less than 10 count(example). In this case, it is very simple, since this list contains only 3 elements, but the real list is a little longer.

This can be done simply with a for loop. But is there a way to do this with some other R function (e.g. rapply)? My problem with these applicable functions is that they only look at one element.

If I do grep to get all the elements dbname, I cannot get the count of each element.

rapply(tmp, function(x) paste("Content: ", x))[grep("dbname", names(rapply(tmp, c)))]

Does anyone have a better idea than a for loop?

+5
3

, , .

x <- tmp[sapply(tmp, function(x){x$count>10})]
str(x)
(the list items you wanted)

, , ,

testForCount <- function(x) {if ('count' %in% names(x)) x$count>10 else FALSE}
tmp[sapply (tmp, count)]

, , . ( , ).

+2

R data.frames, , - ( data.frame , ).

x <- do.call(rbind,tmp)
dat <- data.frame(x)
dat$count <- as.numeric(dat$count)

> dat
    count status     menu   dbname
1 1057230     Ok   PubMed   pubmed
2  305215     Ok      PMC      pmc
3       1     Ok Journals journals

, , data.frame:

> dat$dbname[dat$count<10]
$resultitem
[1] "journals"
+5

It looks like your list comes from an XML structure. Easier to focus on what you want with XPath and using the NodeSet structure and function getNodeSet in the XML package

0
source

All Articles