+4 votes
in Programming Languages by (17.7k points)
I want to create a list of vectors. Is there any R function that I should use for it?

1 Answer

0 votes
by (35.8k points)

The list() function of R can be used to create a list of elements. You can use a vector as an element of the list.

Here is an example:

> a <- c(1,2,3)
> b <- c(11,12,13)
> aa <- list(a,b)
> aa
[[1]]
[1] 1 2 3

[[2]]
[1] 11 12 13

If you want to add a new element to an existing list, you can use [[indx]] where "indx" is the position of the new element.

E.g.

> c <- c(21,22,23)
> aa[[3]] <-c
> aa
[[1]]
[1] 1 2 3

[[2]]
[1] 11 12 13

[[3]]
[1] 21 22 23

...