我在文档中找不到这个,但以下语法适用于data.table
:
dt = data.table(wines)
dt[, !"alcohol", with = F]
如果您愿意,您还可以有一个列列表:
dt[, !c("Country", "alcohol"), with = F]
它刚刚在新闻中记录了 v1.8.4 似乎:
当 with=FALSE 时,“!” 也可以是 j 的前缀,#1384ii。这将选择除命名列之外的所有列。
DF[,-match("somecol",names(DF))]
# works when somecol exists. If not, NA causes an error.
DF[,-match("somecol",names(DF),nomatch=0)]
# works when somecol exists. Empty data.frame when it doesn't, silently.
DT[,-match("somecol",names(DT)),with=FALSE]
# same issues.
DT[,setdiff(names(DT),"somecol"),with=FALSE]
# works but you have to know order of arguments, and no warning if missing
对比
DT[,!"somecol",with=FALSE]
# works and easy to read. With (helpful) warning if somecol isn't there.
但是以上所有内容都复制了除已删除列之外的每一列。更常见的是:
DT[,somecol:=NULL]
通过引用按名称删除列。