I use $ to add a list column to a data.table in R. When the data.table has more than one row, this works as expected.
library(data.table)
dt2 <- data.table(x = 1:2)
dt2$y <- list(c(1, 1), c(2, 2))
dt2
#> x y
#> 1: 1 1,1
#> 2: 2 2,2
However, when the data.table has exactly one row, only the first element of the vector in the list is returned with a warning:
dt1 <- data.table(x = 1)
dt1$y <- list(c(1, 1))
#> Warning in `[<-.data.table`(x, j = name, value = value): Supplied 2 items
#> to be assigned to 1 items of column 'y' (1 unused)
dt1
#> x y
#> 1: 1 1
This seems inconsistent. Is it a feature or a bug?
By contrast, doing the same thing with data.frames returns the expected output, regardless of the number of rows in the data.frame.
df1 <- data.frame(x = 1)
df1$y <- list(c(1, 1))
df1
#> x y
#> 1 1 1, 1
df2 <- data.frame(x = 1:2)
df2$y <- list(c(1, 1), c(2, 2))
df2
#> x y
#> 1 1 1, 1
#> 2 2 2, 2
packageVersion("data.table")?