rlistdata.tablerep

I'd like to put multiples repetition of a list in data.table


I'm trying to create this table:

library(data.table)
table <-  data.table(
    x = "D"  ,
    y = list(c("a", "b"), c("a", "b"),c("a", "b"),c("a", "b"), "test"))
table

I'd like to use rep() with maybe list():

table <- data.table(
    x = "D"  ,
    y = list(rep(c("a", "b"), 4), "test")
  )

But that's not the expected result.


Solution

  • You could replicate a list and use c(...) to make a new list out of the previous list and "test":

    library(data.table)
    table1 <-  data.table(
      x = "D"  ,
      y = list(c("a", "b"), c("a", "b"),c("a", "b"),c("a", "b"), "test"))
    table1
    
    
    table2 <- data.table(
      x = "D"  ,
      y = c(rep(list(c("a", "b")), 4), "test")
    )
    
    identical(table1,table2) 
    [1] TRUE