I wish to expand lists to a given length using rep()
, as in:
n = 5
l = 1:3
rep(l, length=n)
However, my lists come in two flavours, nested or not nested:
l1 <- list(a=1, b=2)
l2 <- list(list(a=1, b=2), list(x=1, y=2))
rep(l2, length=n) # as desired
rep(l1, length=n) # result is a single list, where I really want
rep(list(l1), length=n) # instead
To deal with this problem I probably need to identify the problematic l1
as being "first-level" and wrap it into list()
before applying rep()
. What is the best way to do this?