[R] Easy way to `iris[,-"Petal.Length"]' subsetting?

Liviu Andronic landronimirc at gmail.com
Sat Oct 17 22:49:28 CEST 2009


Dear all
What is the easy way to drop a variable by using its name (and not its
number)? Example:
> data(iris)
> head(iris)
  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
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
> head(iris[,-3])
  Sepal.Length Sepal.Width Petal.Width Species
1          5.1         3.5         0.2  setosa
2          4.9         3.0         0.2  setosa
3          4.7         3.2         0.2  setosa
4          4.6         3.1         0.2  setosa
5          5.0         3.6         0.2  setosa
6          5.4         3.9         0.4  setosa
> head(iris[,-which(names(iris)=="Petal.Length")])
  Sepal.Length Sepal.Width Petal.Width Species
1          5.1         3.5         0.2  setosa
2          4.9         3.0         0.2  setosa
3          4.7         3.2         0.2  setosa
4          4.6         3.1         0.2  setosa
5          5.0         3.6         0.2  setosa
6          5.4         3.9         0.4  setosa
> head(iris[,-"Petal.Length"])
Error in -"Petal.Length" : invalid argument to unary operator

Is there something more straight-forward than
`-which(names(iris)=="Petal.Length")', to drop a variable using a
string?
Thank you
Liviu



-- 
Do you know how to read?
http://www.alienetworks.com/srtest.cfm
Do you know how to write?
http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail




More information about the R-help mailing list