使用data.Table中的反向选择通过引用删除多个列

时间:2017-07-12 10:06:29

标签: r data.table

我想使用引用删除不在列表中的列。

library("data.table")
df <- data.frame("ID"=1:10,"A"=1:10,"B"=1:10,"C"=1:10,"D"=1:10)
setDT(df,key="ID")
list_to_keep <- c("ID","A","B","C")
df[,!names(df)%in%list_to_keep,with=FALSE]

给我一​​些我想要删除的列,但是当我这样做时:

df <- data.frame("ID"=1:10,"A"=1:10,"B"=1:10,"C"=1:10,"D"=1:10)
setDT(df,key="ID")
list_to_keep <- c("ID","A","B","C")
df[,!names(df)%in%list_to_keep:=NULL,with=FALSE]

我得到LHS of := isn't a column names ('character' or positions ('integer' or 'numeric')。这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:2)

我们可以使用setdiff获取不在names中的list_to_keep数据集,并将其分配(:=)到NULL < / p>

df[, setdiff(names(df), list_to_keep) := NULL]

正如@rosscova所提到的,在逻辑which上使用vector可用于获取列的位置并将列分配给NULL

df[, which(!names(df)%in%list_to_keep):=NULL] 

答案 1 :(得分:1)

LHS的{​​p> :=是&#34;列名称(或数字位置)的字符向量或者如此评估的变量。&#34;

!names(df)%in%list_to_keep是逻辑向量。

所以,

df[,names(df)[!names(df)%in%list_to_keep]:=NULL]

会奏效。