有人能解释一下在这个交互运行R
代码中发生了什么吗?我在Ubuntu Linux上使用dplyr 1.1.2
和data.table 1.14.8
.请注意,只有在将索引隐式分配给dt1
行之后,才会出现这种差异.在这种情况下,为什么dt[cond]
给出的结果与subset(dt, cond)
不同?将data.table
和tidyverse
函数组合在一个链中通常是"危险的"吗?
library(data.table)
library(dplyr)
mydt1 = data.table(year=rep(2017:2018, each=3), month=rep(1:3, times=2))
mydt2 = data.table(year=rep(2016:2017, each=3), month=rep(4:6, times=2))
mydt1[year == 2018] # this appears to assigns `year` as an index to mydt1
rbindlist(list(mydt1, mydt2))[year == 2017]
# produces expected output:
# year month
# 1: 2017 1
# 2: 2017 2
# 3: 2017 3
# 4: 2017 4
# 5: 2017 5
# 6: 2017 6
subset(bind_rows(mydt1, mydt2), year == 2017)
# produces the same output as above
bind_rows(mydt1, mydt2)[year == 2017]
# what happens now?
# year month
# 1: 2017 1
# 2: 2017 2
# 3: 2017 3