Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

R write elements of nested list to csv

$
0
0

I have a list of lists which, in turn, have multiple lists in them due to the structure of some JSON files. Every list has the same number (i.e., 48 lists of 1 list, of 1 list, of 1 list, of 2 lists [where I need the first of the last two]). Now, the issue is, I need to retrieve deeply nested data from all of these lists.

For a reproducible example.

The list structure is roughly as follows (maybe one more level):

list1 = list(speech1 = 1, speech2 = 2)
list2 = list(list1, randomvariable="rando")
list3 = list(list2) #container
list4 = list(list3, name="name", stage="stage")
list5 = list(list4) #container
list6 = list(list5, date="date")
listmain1 = list(list6)
listmain2 = list(list6)
listmain3 = list(listmain1, listmain2)

The structure should like like so:

[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]][[1]][[1]]
[[1]][[1]][[1]][[1]][[1]][[1]][[1]]$speech1
[1] 1

[[1]][[1]][[1]][[1]][[1]][[1]][[1]]$speech2
[1] 2


[[1]][[1]][[1]][[1]][[1]][[1]]$randomvariable
[1] "rando"



[[1]][[1]][[1]][[1]]$name
[1] "name"

[[1]][[1]][[1]][[1]]$stage
[1] "stage"



[[1]][[1]]$date
[1] "date"



[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
[[2]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]][[1]][[1]]
[[2]][[1]][[1]][[1]][[1]][[1]][[1]]$speech1
[1] 1

[[2]][[1]][[1]][[1]][[1]][[1]][[1]]$speech2
[1] 2


[[2]][[1]][[1]][[1]][[1]][[1]]$randomvariable
[1] "rando"



[[2]][[1]][[1]][[1]]$name
[1] "name"

[[2]][[1]][[1]][[1]]$stage
[1] "stage"



[[2]][[1]]$date
[1] "date"

The end result would look like this:

    date  name  speech1  speech2   
1    

2

I want to make columns out of the variables which I need and rows out of the lists that I extract them from. In the above example, I would need to retrieve variables speech1, speech2, name, and date from all of the main lists and convert to a simpler dataframe. I'm not quite sure the fastest way to do this and have been knocking my head over with lapply() and purrr for the last couple of days. Ideally, I want to treat the lists as rowIDs with flattened variables in the columns -- but that has also been tricky. Any help is appreciated.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>