0

When trying to store a vector in a data.table, this works only when the data.table has length of more than one.

Please find below a simplified version of the problem

library(data.table)

Working fine

dt <- data.table( a = c("a", "b"), l = list())
dt$l[[1]] <- c(1:3) 

Results in:

   a     l
1: a 1,2,3
2: b

Producing Error

dt <- data.table( a = c("a"), l = list())
dt$l[[1]] <- c(1:3) 

Error in [<-.data.table(x, j = name, value = value) : Supplied 3 items to be assigned to 1 items of column 'l'. The RHS length must either be 1 (single values are ok) or match the LHS length exactly. If you wish to 'recycle' the RHS please use rep() explicitly to make this intent clear to readers of your code.

Expected result in:

   a     l
1: a 1,2,3
Matt
  • 2,947
  • 1
  • 9
  • 21
  • 1
    With the latest version 1.12.3 of data.table (in development), this problem seems to be gone. https://github.com/Rdatatable/data.table/blob/master/NEWS.md says *Assigning to one item of a list column no longer requires the RHS to be wrapped with list or .()* – Uwe Jul 31 '19 at 12:43
  • Relevant [Using lists inside data.table columns](https://stackoverflow.com/questions/22531477/using-lists-inside-data-table-columns) – markus Jul 31 '19 at 12:57

1 Answers1

3

Is this what you're looking for?

dt <- data.table(a = "a", l = list())
dt[1L, l := list(list((1:3)))]

Results in:

   a     l
1: a 1,2,3

Matt
  • 2,947
  • 1
  • 9
  • 21