12

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).

2
  • 13
    Are you looking for rep(list(c(2,6)),8)?
    – nicola
    Commented Mar 1, 2016 at 23:16
  • awesome, thanks, I KNEW it would be something silly. You wanna make this an answer so I can give you the rep?
    – dez93_2000
    Commented Mar 1, 2016 at 23:39

1 Answer 1

26

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.