gpt4 book ai didi

r - 在data.Table中使用反向选择按引用删除多列

转载 作者:行者123 更新时间:2023-12-05 04:10:01 24 4
gpt4 key购买 nike

<分区>

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

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')。正确的做法是什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com