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

Gabor Grothendieck ggrothendieck at gmail.com
Sat Oct 17 23:14:52 CEST 2009


Try this:

subset(iris, select = - Petal.Length)

On Sat, Oct 17, 2009 at 4:49 PM, Liviu Andronic <landronimirc at gmail.com> wrote:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list