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

Time:02-02

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. thanks for your help !

CodePudding user response:

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
  •  Tags:  
  • Related