different results for standard form and functional form of data.table assigne-by-reference `:=`

末鹿安然 提交于 2020-01-01 09:17:22

问题


There seems to be a minor difference between data.tabel's assignment by reference := in the standard to the functinal form.

Standard form coerces RHS to vector, the functional form does not. A detail, but not documented as I believe.

library(data.table)
dt <- data.table(a = c('a','b','c'))
v <- c('A','B','C')
l <- list(v)

all.equal(copy(dt)[, new := v], copy(dt)[, `:=` (new = v)])
# [1] TRUE
all.equal(copy(dt)[, new := l], copy(dt)[, `:=` (new = l)])
# [1] "Datasets have different column modes. First 3: new(character!=list)"

copy(dt)[, new := l][]
#    a new
# 1: a   A
# 2: b   B
# 3: c   C

copy(dt)[, `:=` (new = l)][]
#    a   new
# 1: a A,B,C
# 2: b A,B,C
# 3: c A,B,C

This is a major Edit of how I asked this question originally.


回答1:


This is very good question that touches design decision about := operator.

For simple calls using := as an operator, like col := val, we decided to wrap val into a list automatically. This decision was made to make it more convenient for users to assign single column.

When you are using function call form, ":="(col = val) we are not wrapping val into list anymore. It is extended form already. := behaves as an alias to list but updating in-place. You can always check what will be the updated column by changing := into list (or .) like .(col = val).

Not that even when using := as an operator, you still have to provide RHS as list of you are creating 2+ columns, c("col1","col2") := list(val1, val2).



来源:https://stackoverflow.com/questions/44067091/different-results-for-standard-form-and-functional-form-of-data-table-assigne-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!