这是我遇到的问题的一个例子.我误用了还是这个错误?
require(data.table)
x <- data.table(a = 1:4)
# this does not work
x[ , {b = a + 3; `:=`(c = b)}]
# Error in `:=`(c = b) : unused argument(s) (c = b)
# this works fine
x[ ,`:=`(c = a + 3)]
Run Code Online (Sandbox Code Playgroud)
Ric*_*rta 13
不是一个错误,只是括号的顺序应该是不同的:
也就是说,使用大括号仅包装RHS
参数 `:=`(LHS, RHS)
例:
# sample data
x <- data.table(a = 1:4)
# instead of:
x[ , {b = a + 3; `:=`(c, b)}] # <~~ Notice braces are wrapping LHS AND RHS
# use this:
x[ , `:=`(c, {b = a + 3; b})] # <~~ Braces wrapping only RHS
x
# a c
# 1: 1 4
# 2: 2 5
# 3: 3 6
# 4: 4 7
Run Code Online (Sandbox Code Playgroud)
你可能正在寻找这个:
x[ , c := {b = a + 3; b}]
Run Code Online (Sandbox Code Playgroud)
究竟.使用:=
其他不正确的方式给出了这样的(长)错误:
x := 1
# Error: := is defined for use in j only, and (currently) only once; i.e.,
# DT[i,col:=1L] and DT[,newcol:=sum(colB),by=colA] are ok, but not
# DT[i,col]:=1L, not DT[i]$col:=1L and not DT[,{newcol1:=1L;newcol2:=2L}].
# Please see help(":="). Check is.data.table(DT) is TRUE.
Run Code Online (Sandbox Code Playgroud)
但不是在问题显示的情况下,只给出:
x[ , {b = a + 3; `:=`(c = b)}]
# Error in `:=`(c = b) : unused argument(s) (c = b)
Run Code Online (Sandbox Code Playgroud)
我刚刚在v1.8.9中改变了这一点.这两种不正确的使用方式:=
现在都提供了更简洁的错误:
x[ , {b = a + 3; `:=`(c = b)}]
# Error in `:=`(c = b) :
# := and `:=`(...) are defined for use in j only, in particular ways. See
# help(":="). Check is.data.table(DT) is TRUE.
Run Code Online (Sandbox Code Playgroud)
我们会美化?":="
.感谢@Alex突出显示!