按名称取消选择列 [英] Deselecting a column by name
本文介绍了按名称取消选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法选择数据框的所有列,但具有特定名称的列除外。
Is there a way to select all columns of a data frame except a column that has a particular name.
这将是 df [,-1]
,除了使用列名而不是索引?
It would be the analog of df[, -1]
, except using the column name instead of the index?
推荐答案
您可以使用向量子集来执行此操作。首先,创建一个虚拟数据集:
You can do this using vector subsetting. First, create a dummy data set:
R> dd = data.frame(A = 1:3, B = 1:3, C=1:3, D=1:3)
然后使用!
运算符来反转选择:
Then use the !
operator to reverse the selection:
R> dd[ ,!(colnames(dd) == "A")]
B C D
1 1 1 1
2 2 2 2
3 3 3 3
或者,您可以:
-
略短的版本(由@Tomas提供):
A slightly shorter version (courtesy of @Tomas):
dd[ , names(dd) != "A"]
应对多列(@Tyler提供)
To cope with multiple columns (courtesy of @Tyler)
dd[ ,!(colnames(dd) %in% c("A", "B"))]
这篇关于按名称取消选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文