Skip to content

Adding list column to one row data table #3626

@jakob-r

Description

@jakob-r

I can not add a list column to a data.table with one row:

library(data.table)
dt = data.table(a = 1)
list_column = list(list(a = 1, b = 2))
# does not work:
dt$b = list_column
Error in set(x, j = name, value = value) : 
  Supplied 2 items to be assigned to 1 items of column 'b'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.
dt[, b := list_column]
Error in `[.data.table`(dt, , `:=`(b, list_column)) : 
  Supplied 2 items to be assigned to 1 items of column 'b'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.

The following workaround works and prodcues the desired result.

dtb = data.table(b = list_column)
cbind(dt, dtb)
       a      b
   <num> <list>
1:     1 <list>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions