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

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

On Mar 6, 2011, at 7:34 PM, David Winsemius wrote:

>
> On Mar 6, 2011, at 6:05 PM, Liviu Andronic wrote:
>
>> On Sun, Mar 6, 2011 at 11:53 PM, Liviu Andronic <landronimirc_at_gmail.com
>> > wrote:
>>> On Sun, Mar 6, 2011 at 11:49 PM, Liviu Andronic <landronimirc_at_gmail.com
>>> > 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))),])
>>>> ##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))),])
>>>>
>>> And of course I found the culprit after sending the e-mail: wrong
>>> call
>>> order. The following does what I want, although it's a bit messy:
>>> xa <- with(x, x[Sepal.Length==6.7,]); with(xa,
>>> xa[rev(order(sort(Species1))),])
>>> xa <- subset(x, Sepal.Length==6.7); with(xa,
>>> xa[rev(order((sort(Species1)))),])
>>>
>> But it still doesn't work on my data! Any ideas for a different
>> approach?
>
> subset(x[order(x$Species1), ], Sepal.Length==6.7 )
>
> Slight modification of the order/sort first subset second strategy:
>
> xa <- x[order(x$Species1), ][x$Sepal.Length==6.7, ]

If you really wanted to select cases first, and still keep it a one- liner there is the option of:

(y <- x[x$Sepal.Length==6.7, ])[order(y$Species1), ]

>
>> Liviu
>>
>>
>>> Regards
>>> Liviu
>>>
>>>
>>>> I've checked The R Inferno, Quick-R and several other places with
>>>> no
>>>> obvious solution.
>>>>
>>>> Any ideas? Regards
>>>> Liviu
>>>>
>>>>
>>>> --
>>>> Do you know how to read?
>>>> http://www.alienetworks.com/srtest.cfm
>>>> http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
>>>> Do you know how to write?
>>>> http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
>>>>
>>>
>>>
>>>
>>> --
>>> Do you know how to read?
>>> http://www.alienetworks.com/srtest.cfm
>>> http://goodies.xfce.org/projects/applications/xfce4-dict#speed-
>>> reader
>>> Do you know how to write?
>>> http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
>>>
>>
>>
>>
>> --
>> Do you know how to read?
>> http://www.alienetworks.com/srtest.cfm
>> http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
>> 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.
>
> 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.

David Winsemius, MD
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 Mon 07 Mar 2011 - 08:59:00 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 Mon 07 Mar 2011 - 09:10:20 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