Accessing vectors in a vector/list


How do I access vectors inside of another vector (or another array) in a way that I can save values inside it?
set <- c(a, b, c, d, e); while a, b, c, d, e are defined as vectors.
I want to access vector a, so I can save values in it and select it separately.

I have a loop in my script that practically does the same thing with different inputs and my intention is to save the output of each iteration in a different vector. Probably a rather basic problem, I'm quite new in R and so far I couldn't find a solution.


Hi and welcome! I think you might be looking for lists instead of vectors if you want to store multiple vectors in one object. Here's an example that creates a named list of 3 vectors and then you can use [] to access each of the list elements. If want to access a vector within a list you can use [[]].

a <- 1:5
b <- 6:10
c <- 11:15

set <- list(a = a, b = b, c = c)

#> $a
#> [1] 1 2 3 4 5
#> $b
#> [1]  6  7  8  9 10
#> [1] 11 12 13 14 15

Created on 2020-05-21 by the reprex package (v0.3.0)

Here's some more info about lists from one of Hadley's books:

Alright, thanks a lot!

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