我想使用引用删除不在列表中的列。
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')
。这样做的正确方法是什么?
答案 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]
会奏效。