考虑数据帧df1,它被设置为数据表。请注意,未设置密钥。
library(data.table)
df1 <- data.frame(v1 = letters[seq( from = 1, to = 4 )])
setDT(df1)
tables() # verify that df1 is now data table. No key.
NAME NROW NCOL MB COLS KEY
[1,] df1 4 1 1 v1
Total: 1MB
df2 <- df1 # create df2 as copy of df1
tables()
NAME NROW NCOL MB COLS KEY
[1,] df1 4 1 1 v1
[2,] df2 4 1 1 v1 # two data tables. No keys.
Total: 2MB
setkey(df2, v1) # set key to df2 (only)
tables()
NAME NROW NCOL MB COLS KEY
[1,] df1 4 1 1 v1 v1 # both df1 and df2 are assigned keys
[2,] df2 4 1 1 v1 v1 # it is as if df1 is assigned key via df2 ('retrospectively')
Total: 2MB
但是,如果我在设置密钥之前对df2进行了更改,比如添加一个列,则setkey()的行为会有所不同:不会是&#34;回顾&#34;。 (见下面的代码)。 我想知道为什么会这样。感谢
library(data.table)
df1 <- data.frame(v1 = letters[seq( from = 1, to = 4 )])
setDT(df1)
tables()
NAME NROW NCOL MB COLS KEY
[1,] df1 4 1 1 v1
Total: 1MB
df2 <- df1
tables()
NAME NROW NCOL MB COLS KEY
[1,] df1 4 1 1 v1
[2,] df2 4 1 1 v1
Total: 2MB
df2$v2 <- c(1:4) # add column, therefore v2 != v1
setkey(df2, v1)
tables()
NAME NROW NCOL MB COLS KEY
[1,] df1 4 1 1 v1 # in this instance v1 wasn't set a key via df2
[2,] df2 4 2 1 v1,v2 v1
Total: 2MB