# How to unlist many lists at the same time?

Hi!
I am new to R and dealing with a large array. As you can see from the image, the 52nd item in my array is a list [1x1x1]. It contains another list [1x113], and each one of the 113 elements contains a double [n x 1].
I can extract each 'double' by unlisting like so:

data=unlist(mylist[[52]] [[1]] [[2]])

"data" here is the numeric vector that I need (essentially my data).

My question is: Can I do it for many 'doubles' at once? Instead of doing the same thing over and over like:

data2=unlist(mylist[[52]] [[1]] [[2]])
data3=unlist(mylist[[52]] [[1]] [[3]])
data4=unlist(mylist[[52]] [[1]] [[4]])
data5=unlist(mylist[[52]] [[1]] [[5]]) .... and so on???

Ideally, I'd like to specify which elements I want to extract, for example [[52]] [[1]] [[3, 5]]

Sorry if I was not 100% clear but I am still trying to learn the R lingo... -_-

Thanks!!!

Without a `reprex`. See the FAQ, I'll just offer a top-of-the-head.

``````# Have a chatbot write a program in the r programming language
# to create a list of 52 elements each of which contains
# 113 lists each of which contains a list each of which
# contains a random integer in the range 10000 99999
# this one is from openai.com

# Set the number of elements, sublists, and nested sublists
num_elements <- 52
num_sublists <- 113
num_nested_sublists <- 1

# Create an empty list to hold the output
output_list <- list()

# Loop over the number of elements and create a sublist for each
for (i in 1:num_elements) {

# Create an empty sublist to hold the nested sublists
sublist <- list()

# Loop over the number of sublists and create a nested sublist for each
for (j in 1:num_sublists) {

# Create an empty nested sublist to hold the random integer
nested_sublist <- list()

# Generate a random integer in the desired range and add it to the nested sublist
random_int <- sample(10000:99999, size = 1)
nested_sublist[[1]] <- random_int

# Add the nested sublist to the current sublist
sublist[[j]] <- nested_sublist
}

# Add the current sublist to the output list
output_list[[i]] <- sublist
}

# one way to do it
take <- function(x,y) output_list[x][[1]][[y]][[1]]
mapply(take,52,c(3,5))
#> [1] 13040 83481
``````

Created on 2023-03-21 with reprex v2.0.2

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.