You can't remove a column by name with a -
sign, like you can with numerical indices.
But you can easily remove a column by name by using logical indexing. Here's an example, removing the column Sepal.Width
from iris
:
head(iris, 2)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
i <- iris[,names(iris) != 'Sepal.Width']
head(i, 2)
Sepal.Length Petal.Length Petal.Width Species
1 5.1 1.4 0.2 setosa
2 4.9 1.4 0.2 setosa
Note that -
is not used, and the column name is quoted.