11
多列是否有可能在同一個語句更新data.table超過1列?更新的data.table
喜歡的東西
dt[,onecol:=1 anothercol:=2]
我見過一些例子,他們做
dt[,c("onecol","anothercol"):=1]
,但我不知道如何不同的公式分配給每一列
多列是否有可能在同一個語句更新data.table超過1列?更新的data.table
喜歡的東西
dt[,onecol:=1 anothercol:=2]
我見過一些例子,他們做
dt[,c("onecol","anothercol"):=1]
,但我不知道如何不同的公式分配給每一列
是的,這
library(data.table)
dt <- data.table(a=1:4, b=4:1)
dt[,c("a", "b") := list(min(a), max(b))]
# dt
# a b
# 1: 1 4
# 2: 1 4
# 3: 1 4
# 4: 1 4
:自1.8.4版本已經可以
或者,做同樣的事情在一個更可讀的方式:
dt <- data.table(a=1:4, b=4:1)
dt[,`:=`(a = min(a),
b = max(a))]
dt
# a b
# 1: 1 4
# 2: 1 4
# 3: 1 4
# 4: 1 4
搜索the current data.table NEWS file爲「多LHS」看,這也將與by=
參數工作。
FWIW,在'?「:=」'幫助頁是有點過時它什麼可以用'做說明:='。它的「實施例」部分不包括這裏所示的語法的例子,和「參數」部分意味着這僅適用時與'= FALSE'。 –