gpt4 book ai didi

r - data.table 链接不会创建新变量

转载 作者:行者123 更新时间:2023-12-04 07:24:00 25 4
gpt4 key购买 nike

我不能在这个例子中进行链接工作。有人可以解释我错过了什么吗?

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

关于r - data.table 链接不会创建新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68316839/

25 4 0