17

I'm just learning R and having a hard time wrapping my head around how to extract elements from objects in a list. I've parsed a json file into R giving me list object. But I can't figure out how, from there, to extract various json elements from the list. here's a truncated look at how my data appears after parsing the json:

 > #Parse data into R objects#
 > list.Json= fromJSON(,final.name, method = "C")
 > head(listJson,6)
[[1]]
[[1]]$contributors
NULL

[[1]]$favorited
[1] FALSE

...[truncating]...
[[5]]
[[5]]$contributors
NULL

[[5]]$favorited
[1] FALSE

I can figure out how to extract the favorites data for one of the objects in the list

> first.object=listJson[1]
> ff=first.object[[1]]$favorited
> ff
[1] FALSE

But I'm very confused about how to extract favorited for all objects in the list. I've looked into sappily, is that the correct approach? Do I need to put the above code into a for...next loop?

Martin
  • 1,570
  • 3
  • 19
  • 32
  • Lists are a little confusing in R. They're actually more akin to hashmaps/dictionaries from other languages. Have a look at http://stackoverflow.com/questions/2050790/how-to-correctly-use-lists-in-r to get some insight. – Wilduck Jul 17 '12 at 20:25
  • 1
    Converting JSON to a `data.frame` can be a pain, especially if the keys from one object to the next are not guaranteed to be the same. I liked @Jeff Allen's answer, but be careful when your missing values are `NULL` instead of `NA`. (You can control the default using `fromJSON(..., nullValue = NULL)` if you're using `RJSONIO`). For example, `sapply(listJson, "[[", "contributors")` returns a list when its missing values are `NULL` but returns a vector when its missing values are 'NA'. – lockedoff Jul 17 '12 at 21:36

1 Answers1

35

sapply is going to apply some function to every element in your list. In your case, you want to access each element in a (nested) list. sapply is certainly capable of this. For instance, if you want to access the first child of every element in your list:

sapply(listJson, "[[", 1)

Or if you wanted to access the item named "favorited", you could use:

sapply(listJson, "[[", "favorited")

Note that the [ operator will take a subset of the list you're working with. So when you access myList[1], you still have a list, it's just of length 1. However, if you reference myList[[1]], you'll get the contents of the first space in your list (which may or may not be another list). Thus, you'll use the [[ operator in sapply, because you want to get down to the contents of the list.

Jeff Allen
  • 17,277
  • 8
  • 49
  • 70
  • Thanks, getting my head wrapped around R lists has been a challenge. I was ultimately able to also get this working with a for loop:t1=NULL for (i in 1:length(list.Json)) { check=list.Json[[i]]$favorited if(typeof(check)=="NULL"){ check=list(NULL) } t1=c(t1,check) } – Martin Jul 18 '12 at 23:35
  • 2
    that's just weird, but really useful – kpierce8 Oct 21 '14 at 18:05