Re: [R] sorting & subsetting a data.frame

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sun, 06 Mar 2011 18:08:52 -0500

On Mar 6, 2011, at 5:49 PM, Liviu Andronic wrote:

> Dear all
> This may be obvious, but I cannot get it working. I'm trying to subset
> & sort a data frame in one go.
> x <- iris
> x$Species1 <- as.character(x$Species)
> ##subsetting alone works fine
> with(x, x[Sepal.Length==6.7,])
> ##sorting alone works fine
> with(x, x[order(Sepal.Length, rev(sort(Species1))),])
> ##gets subsetted, but not sorted as expected
> with(x, x[(Sepal.Length==6.7) & order(Sepal.Length,
> rev(sort(Species1))),])

You are doing logical operations between a logical and numeric vector. You should get a bunch of TRUE's and FALSE's and it might be as you say, strange. (The ordering will not occur.)

> ##gets subsetted, but sorts very strangely
> xa <- with(x, x[Sepal.Length==6.7,]); with(xa, xa[order(Sepal.Length,
> rev(sort(Species1))),])
> xa <- with(x, x[Sepal.Length==6.7,]); with(xa,
> xa[order(rev(sort(Species1))),])
>
> I've checked The R Inferno, Quick-R and several other places with no
> obvious solution.
>
> Any ideas? Regards
> Liviu
>
> --

David Winsemius, MD
Heritage Laboratories
West Hartford, CT



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. Received on Sun 06 Mar 2011 - 23:11:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 06 Mar 2011 - 23:40:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive