You can use a simple list of names :
(您可以使用简单的名称列表:)
DF <- data.frame(
x=1:10,
y=10:1,
z=rep(5,10),
a=11:20
)
drops <- c("x","z")
DF[ , !(names(DF) %in% drops)]
Or, alternatively, you can make a list of those to keep and refer to them by name :
(或者,您也可以列出要保留的内容,并按名称引用它们:)
keeps <- c("y", "a")
DF[keeps]
EDIT : For those still not acquainted with the drop
argument of the indexing function, if you want to keep one column as a data frame, you do:
(编辑:对于那些仍不熟悉索引函数的drop
参数的用户,如果要保留一列作为数据框,请执行以下操作:)
keeps <- "y"
DF[ , keeps, drop = FALSE]
drop=TRUE
(or not mentioning it) will drop unnecessary dimensions, and hence return a vector with the values of column y
.
(drop=TRUE
(或不提及它)将删除不必要的尺寸,因此返回带有y
列值的向量。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…