1

I often need to store complex object in data.table so I used list column a lot. Sometimes I need to initialize the column with empty list, and sometimes I need to copy a list column to another column.

At first there seemed to be nothing wrong with these:

# the example at https://stackoverflow.com/a/22536321/3718827
dt = data.table(id = 1:2, comment = vector("list", 2L))
# my usage need to add column to existing dt, not creating a data.table from scratch, so I modified it a little bit
dt <- data.table(id = 1:2)
dt[, comment := vector("list", nrow(dt))]
dt[1, comment := .(list(list(a = "a", b = "b")))]
dt[, edited_comment := comment]

However if there is only one row, the code above no longer work

> dt <- data.table(id = 1)
> dt[, comment := vector("list", nrow(dt))]
Warning message:
In `[.data.table`(dt, , `:=`(comment, vector("list", nrow(dt)))) :
  Adding new column 'comment' then assigning NULL (deleting it).
> dt[1, comment := .(list(list(a = "a", b = "b")))]
> dt[, edited_comment := comment]
Warning message:
In `[.data.table`(dt, , `:=`(edited_comment, comment)) :
  Supplied 2 items to be assigned to 1 items of column 'edited_comment' (1 unused)

I tried a different syntax and this seemed to work

> dt <- data.table(id = 1)
> dt[, comment := .(list(NULL))]
> dt[1, comment := .(list(list(a = "a", b = "b")))]
> dt[, edited_comment := list(comment)]

For the list column initialization, maybe the example code I found is only meant for data.table() call. To add a column it should not be used.

For copying a column, the behavior is understandable but kind of surprising when it worked with nrow > 1 but not working with nrow = 1.

My question is, what's the proper syntax for the task?

And for the surprising behavior difference with nrow >1/=1 of first version, is that expected behavior or a bug?

dracodoc
  • 2,603
  • 1
  • 23
  • 33

0 Answers0