Search code examples
rlistvectorlapplyrep

Create list of vectors using rep()


I want to create a list which is eight times the vector c(2,6), i.e. a list of 8 vectors.

  • WRONG: object = as.list(rep(c(2,6),8)) results instead in a list of 16 single numbers: 2 6 2 6 2 6 2 6 ...
  • I tried drop=0 but that didn't help, and I can't get lapply to work.

(My context: I have a function in which a subfunction will call a numbered list object. The number will be in a loop and therefore change, and the number and loop size is dependent on user values so I don't know what it'll be. If the user doesn't enter a list of vector values for one of the variables, I need to set a default.

The subfunction is expecting e.g. c(2,6) The subfunction is currently looping 8 times so I need a list which is eight times c(2,6).


Solution

  • rep(list(c(2,6)),8) is the answer - thanks to Nicola in comments.