Business professionals discussing data charts and graphs in a modern office setting.

Assemble a Listing of Lists in R (With Instance)

Posted on
banner 336x280

You’ll be able to utility please see modest syntax to assemble an inventory of lists in R:

#outline lists
list1 <- listing(a=5, b=3)
list2 <- listing(c="A", d='B')

#assemble listing of lists
list_of_lists <- listing(list1, list2) 

Refer to instance displays tips on how to utility this syntax in follow.

banner 468x60

Instance: Assemble Listing of Lists in R

Refer to code displays tips on how to assemble an inventory that comprises 3 lists in R:

#outline lists
list1 <- listing(a=5, b=3)
list2 <- listing(c="A", d=c('B', 'C'))
list3 <- listing(e=c(20, 5, 8, 16))

#assemble listing of lists
list_of_lists <- listing(list1, list2, list3)

#view the listing of lists
list_of_lists

[[1]]
[[1]]$a
[1] 5

[[1]]$b
[1] 3


[[2]]
[[2]]$c
[1] "A"

[[2]]$d
[1] "B" "C"


[[3]]
[[3]]$e
[1] 20  5  8 16

We will nearest utility unmarried brackets [ ] to get admission to a selected listing.

As an example, we will be able to utility please see syntax to get admission to the second one listing:

#get admission to 2nd listing
list_of_lists[2]

[[1]]
[[1]]$c
[1] "A"

[[1]]$d
[1] "B" "C"

We will additionally utility double brackets [[ ]] and the greenback signal operator $ to get admission to a selected component inside of a selected listing.

As an example, we will be able to utility please see syntax to get admission to component d inside of the second one listing:

#get admission to component 'd' inside of 2nd listing
list_of_lists[[2]]$d

[1] "B" "C"

You’ll be able to utility related syntax to get admission to any component inside of any listing.

Alternative Sources

Refer to tutorials give an explanation for tips on how to carry out alternative ordinary duties with lists in R:

Convert a Listing to a Knowledge Body in R
Append Values to Listing in R

banner 336x280

Leave a Reply

Your email address will not be published. Required fields are marked *