Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
可能重复: 删除列 R 数据框
假设一个矩阵有 3 个名为“A”、“B”、C”的列,我可以像这样取消选择“B”和“C”列:
df[, -c(2, 3)]
但是如果我想使用列名怎么办?我怎样才能df [,-c(“B”,“C”)]?
匹配是你的朋友:
R> DF <- data.frame(A=1:2, B=2:3, C=3:4) R> DF[, !(colnames(DF) %in% c("B","C")), drop=FALSE] A 1 1 2 2 R>
关键是您需要用于索引的布尔向量(或者数字索引)。所以任何创建布尔值的表达式都可以。