data.table 链接不创建新变量
data.table chaining doesn't create the new variable
我无法在这个例子中进行链接。有人可以解释我错过了什么吗?
library(data.table)
dt <- data.table(a=c(rep("komm", 5), rep("by", 5)), paste0("nr.",1:10))
dt[a=="komm", v3:=sub("nr.", "", V2)]
dt[, v4:=sub("\D*(\d)", "\1", V2)]
# doesn't work
dt[a=="by"][
, v5:=sub("nr.", "no.", V2)][
, v6:=sub("\D*(\d)", "\1", V2)]
我期待得到这个输出
a V2 v3 v4 v5 v6
1: komm nr.1 1 1 <NA> <NA>
2: komm nr.2 2 2 <NA> <NA>
3: komm nr.3 3 3 <NA> <NA>
4: komm nr.4 4 4 <NA> <NA>
5: komm nr.5 5 5 <NA> <NA>
6: by nr.6 <NA> 6 no.6 6
7: by nr.7 <NA> 7 no.7 7
8: by nr.8 <NA> 8 no.8 8
9: by nr.9 <NA> 9 no.9 9
10: by nr.10 <NA> 10 no.10 10
过滤并关闭括号后,就地分配将被破坏。也就是说,DT[cond,newvar:=1]
根据条件赋值,相当于 DT[,newvar:=fifelse(cond,1,newvar)]
或类似的。然而,DT[cond,]
正在返回一个新框架,现在对它的任何工作都完全独立于原始 DT
。
要么做条件赋值两次
dt <- data.table(a=c(rep("komm", 5), rep("by", 5)), paste0("nr.",1:10))
dt[a=="by", v3 := sub("nr.", "", V2)][a=="by", v4:=sub("\D*(\d)", "\1", V2)]
# a V2 v3 v4
# <char> <char> <char> <char>
# 1: komm nr.1 <NA> <NA>
# 2: komm nr.2 <NA> <NA>
# 3: komm nr.3 <NA> <NA>
# 4: komm nr.4 <NA> <NA>
# 5: komm nr.5 <NA> <NA>
# 6: by nr.6 6 6
# 7: by nr.7 7 7
# 8: by nr.8 8 8
# 9: by nr.9 9 9
# 10: by nr.10 10 10
...或一次多重赋值:
dt <- data.table(a=c(rep("komm", 5), rep("by", 5)), paste0("nr.",1:10))
dt[a=="by", c("v3", "v4") := .(sub("nr.", "", V2), sub("\D*(\d)", "\1", V2))]
# a V2 v3 v4
# <char> <char> <char> <char>
# 1: komm nr.1 <NA> <NA>
# 2: komm nr.2 <NA> <NA>
# 3: komm nr.3 <NA> <NA>
# 4: komm nr.4 <NA> <NA>
# 5: komm nr.5 <NA> <NA>
# 6: by nr.6 6 6
# 7: by nr.7 7 7
# 8: by nr.8 8 8
# 9: by nr.9 9 9
# 10: by nr.10 10 10
我无法在这个例子中进行链接。有人可以解释我错过了什么吗?
library(data.table)
dt <- data.table(a=c(rep("komm", 5), rep("by", 5)), paste0("nr.",1:10))
dt[a=="komm", v3:=sub("nr.", "", V2)]
dt[, v4:=sub("\D*(\d)", "\1", V2)]
# doesn't work
dt[a=="by"][
, v5:=sub("nr.", "no.", V2)][
, v6:=sub("\D*(\d)", "\1", V2)]
我期待得到这个输出
a V2 v3 v4 v5 v6
1: komm nr.1 1 1 <NA> <NA>
2: komm nr.2 2 2 <NA> <NA>
3: komm nr.3 3 3 <NA> <NA>
4: komm nr.4 4 4 <NA> <NA>
5: komm nr.5 5 5 <NA> <NA>
6: by nr.6 <NA> 6 no.6 6
7: by nr.7 <NA> 7 no.7 7
8: by nr.8 <NA> 8 no.8 8
9: by nr.9 <NA> 9 no.9 9
10: by nr.10 <NA> 10 no.10 10
过滤并关闭括号后,就地分配将被破坏。也就是说,DT[cond,newvar:=1]
根据条件赋值,相当于 DT[,newvar:=fifelse(cond,1,newvar)]
或类似的。然而,DT[cond,]
正在返回一个新框架,现在对它的任何工作都完全独立于原始 DT
。
要么做条件赋值两次
dt <- data.table(a=c(rep("komm", 5), rep("by", 5)), paste0("nr.",1:10))
dt[a=="by", v3 := sub("nr.", "", V2)][a=="by", v4:=sub("\D*(\d)", "\1", V2)]
# a V2 v3 v4
# <char> <char> <char> <char>
# 1: komm nr.1 <NA> <NA>
# 2: komm nr.2 <NA> <NA>
# 3: komm nr.3 <NA> <NA>
# 4: komm nr.4 <NA> <NA>
# 5: komm nr.5 <NA> <NA>
# 6: by nr.6 6 6
# 7: by nr.7 7 7
# 8: by nr.8 8 8
# 9: by nr.9 9 9
# 10: by nr.10 10 10
...或一次多重赋值:
dt <- data.table(a=c(rep("komm", 5), rep("by", 5)), paste0("nr.",1:10))
dt[a=="by", c("v3", "v4") := .(sub("nr.", "", V2), sub("\D*(\d)", "\1", V2))]
# a V2 v3 v4
# <char> <char> <char> <char>
# 1: komm nr.1 <NA> <NA>
# 2: komm nr.2 <NA> <NA>
# 3: komm nr.3 <NA> <NA>
# 4: komm nr.4 <NA> <NA>
# 5: komm nr.5 <NA> <NA>
# 6: by nr.6 6 6
# 7: by nr.7 7 7
# 8: by nr.8 8 8
# 9: by nr.9 9 9
# 10: by nr.10 10 10