0

I would like to make a dataframe from a list of n. Each list contains 3 different list inside. I am only intrested in 1 list of those 3 list inside. The list I am intrested in is a data.frame with 12 obs of 12 variables.

My imput tmp in my lapply function is a list of n with each 5 observations. 2 of those observations are the Latitude and Longitude. This is how my lapply function looks like:

DF_Google_Places<- lapply(tmp, function(tmp){

Latitude<-tmp$Latitude

Longitude<-tmp$Longitude

LatLon<- paste(Latitude,Longitude, sep=",")

res<-GET(paste("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=",LatLon,"&radius=200&types=food&key=AIzaSyDS6usHdhdoGIj0ILtXJKCjuj7FBmDEzpM", sep=""))

  jsonAnsw<-content(res,"text")

  myDataframe<- jsonlite::fromJSON(content(res,"text"))

})

My question is: how do I get this list of 12 obs of 12 variables into a dataframe from a list of n ?

Could anyone help me out?, Thanks

Sotos
  • 51,121
  • 6
  • 32
  • 66
M. Kooi
  • 245
  • 3
  • 17
  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Mar 30 '16 at 14:28
  • maybe using lapply() and Reduce(). Something along the lines of: Reduce(function(...) merge(..., all=TRUE), lapply(list_of_lists, function(x) x[[df_number]]) )) – zacdav Mar 30 '16 at 14:41
  • I added some more text which contains my lapply function – M. Kooi Mar 30 '16 at 14:44
  • Can you show another couple of examples of the objects you're working with, maybe `str(x)` where `x` is your object. Depending on the structure of it e.g. with `x <- list(a=list(b=1,c=2),d=list(b=3,c=4))` you might try something like this: `y <- do.call(data.frame, list(sapply(names(x[[1]]), function(t) sapply(1:length(x),function(j) x[[j]][[t]]))))` – Philip Mar 30 '16 at 15:18

1 Answers1

0

I'm just posting my comment as an answer so I can show output to show you the idea:

x <- list(a=list(b=1,c=2),d=list(b=3,c=4))

So x is a nested list structure, in this case with consistent naming / structure one level down.

> x
$a
$a$b
[1] 1

$a$c
[1] 2


$d
$d$b
[1] 3

$d$c
[1] 4

Now we'll use do.call to build the data.frame. We need to pass it a named list of arguments, so we'll use list(sapply to get the named list. We'll walk the higher level of the list by position, and the inner level by name since the names are consistent across sub-lists at the inner level. Note here that the key idea is essentially to reverse what would be the intuitive way of indexing; since I want to pull observations at the second level from across observations at the first level, the inner call to sapply traverses multiple values of level one for each value of the name at level two.

y <- do.call(data.frame, 
             list(sapply(names(x[[1]]), 
                         function(t) sapply(1:length(x),
                                            function(j) x[[j]][[t]]))))

> y
  b c
1 1 2
2 3 4

Try breaking apart the command to see what each step does. If there is any consistency in your sub-list structure, you should be able to adapt this approach to walk that structure in the right order and fold the data you need.

On a large dataset, this would not be efficient, but for 12x12 it should be fine.

Philip
  • 7,253
  • 3
  • 23
  • 31