“向上”按钮错误输入导致的错误:
require(data.table)
DT = data.table(x=rep(letters[1:3], each=3L), y=c(1,3,6), v=1:9, key="x,y")
DT[ , z := 42L][] # add 'z' column by reference
DT[ , z := NULL][] # remove "z" column
DT["a", z := NULL][] # attempt to re-remove "z" for certain rows -- error
# Error in `[.data.table`(DT, "a", `:=`(z, NULL)) :
# When deleting columns, i should not be provided
# In addition: Warning message:
# In `[.data.table`(DT, "a", `:=`(z, NULL)) :
# Adding new column 'z' then assigning NULL (deleting it).
DT[, z := NULL][]
# Error in ans[[target]] : subscript out of bounds
DT
# x y v z
# 1: a 1 42 NULL
# 2: a 3 42 NULL
# 3: a 6 42 NULL
# 4: b 1 4 NULL
# 5: b 3 5 NULL
# 6: b 6 6 NULL
# 7: c 1 7 NULL
# 8: c 3 8 NULL
# 9: c 6 9 NULL
如何以简单的方式删除“z”列?
不喜欢
DT <- DT[, 1:3, with=F].
答案 0 :(得分:3)
在最新版本v1.9.6
上,这就是我得到的:
require(data.table) # v1.9.6
DT[, z := 42] ## adds 'z' by reference
DT[, z := NULL] ## removes 'z' column
DT["a", z := NULL] ## sensible error, as the operation doesn't make sense
# Error in `[.data.table`(DT, "a", `:=`(z, NULL)) :
# When deleting columns, i should not be provided
DT[, z := NULL]
# Warning message:
# In `[.data.table`(DT, , `:=`(z, NULL)) :
# Adding new column 'z' then assigning NULL (deleting it).
DT
# x y v
# 1: a 1 1
# 2: a 3 2
# 3: a 6 3
# 4: b 1 4
# 5: b 3 5
# 6: b 6 6
# 7: c 1 7
# 8: c 3 8
# 9: c 6 9
在询问不受欢迎的行为时,必须提供您的版本。它很可能在最近的版本中修复。因此,节省大家时间的最佳方法是首先升级到最新版本,在那里进行测试,然后在此处发布。如果您希望真正提供帮助,那么您还可以在devel version上对其进行测试,如果您能够重现不良行为,则会提出问题。