You can easily make lists of lists
list1 <- list(a = 2, b = 3)
list2 <- list(c = "a", d = "b")
mylist <- list(list1, list2)
mylist is now a list that contains two lists. To access list1 you can use mylist[[1]]. If you want to be able to something like mylist$list1 then you need to do somethingl like
mylist <- list(list1 = list1, list2 = list2)
# Now you can do the following
mylist$list1
Edit: To reply to your edit. Just use double bracket indexing
a <- list_all[[1]]
a[[1]]
#[1] 1
a[[2]]
#[1] 2
Answer from Dason on Stack OverflowYou can easily make lists of lists
list1 <- list(a = 2, b = 3)
list2 <- list(c = "a", d = "b")
mylist <- list(list1, list2)
mylist is now a list that contains two lists. To access list1 you can use mylist[[1]]. If you want to be able to something like mylist$list1 then you need to do somethingl like
mylist <- list(list1 = list1, list2 = list2)
# Now you can do the following
mylist$list1
Edit: To reply to your edit. Just use double bracket indexing
a <- list_all[[1]]
a[[1]]
#[1] 1
a[[2]]
#[1] 2
Using your example::
list1 <- list()
list1[1] = 1
list1[2] = 2
list2 <- list()
list2[1] = 'a'
list2[2] = 'b'
list_all <- list(list1, list2)
Use '[[' to retrieve an element of a list:
b = list_all[[1]]
b
[[1]]
[1] 1
[[2]]
[1] 2
class(b)
[1] "list"
Handling lists of lists (of lists)
List of lists of lists parsing
Converting list of lists into data frame
library(jsonlite)
library(purrr)
library(data.table)
# This isn't strictly json - it's a file where each line is json
# Read data line by line
raw <- readLines("https://files.pushshift.io/reddit/comments/sample_data.json")
# Convert each line from json to a list
json_list <- map(raw, fromJSON)
# Convert each list to a data.table
dt_list <- map(json_list, as.data.table)
# Harness the power of rbind list
dt <- rbindlist(dt_list, fill = TRUE)
# Done
dt
# Here is the same thing in a single pipeline
dt2 <- readLines("https://files.pushshift.io/reddit/comments/sample_data.json") %>%
map(fromJSON) %>%
map(as.data.table) %>%
rbindlist(fill = TRUE)
# Happy to explain any steps - enjoy
Handling lists of lists (of lists)
I often use map() in my code, which gives output in list format. My data can be in the form of a list of lists containing dataframes, so I'm often getting the results of my code functionals in the form of lists of lists of lists (of vectors).
I find these multi-lists hard to manipulate and also to present results clearly when they're in this form. Is the many-list hierarchy a common side-effect of working in R? What are your common strategies for working with layers of lists?
I often use map() in my code, which gives output in list format. My data can be in the form of a list of lists containing dataframes, so I'm often getting the results of my code functionals in the form of lists of lists of lists (of vectors).
I find these multi-lists hard to manipulate and also to present results clearly when they're in this form. Is the many-list hierarchy a common side-effect of working in R? What are your common strategies for working with layers of lists?
Hello, I parsed a son file in R. Now I have an issue that cannot resolve as I have the issue of extracting this lists of lists of lists that also have different row numbers each list and also different columns as, depending on the object, some columns may not be there. How best access these dataframes buried into lists and also these columns hidden into dataframes nested into lists? I am really struggling. Thanks.