[R] Subset and order

David Carlson dcarlson at tamu.edu
Fri Jul 5 21:56:34 CEST 2013


It may be that single and efficient are opposing goals. Two steps
lets you create the subset and then just order each query.
Alternatively, if the data do not change often, create an ordered
version and query that.


David Carlson

----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of Noah Silverman
Sent: Friday, July 5, 2013 2:47 PM
To: Rui Barradas
Cc: R-help at r-project.org
Subject: Re: [R] Subset and order

That would work, but is painfully slow.  It forces a new sort of the
data with every query.  I have 200,000 rows and need almost a
hundred queries.

Thanks,

-N


On Jul 5, 2013, at 12:43 PM, Rui Barradas <ruipbarradas at sapo.pt>
wrote:

> Hello,
> 
> Maybe like this?
> 
> subset(x[order(x$a), ], b == 3)
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Em 05-07-2013 20:33, Noah Silverman escreveu:
>> Hello,
>> 
>> I have a data frame with several columns.
>> 
>> I'd like to select some subset *and* order by another field at
the same time.
>> 
>> Example:
>> 
>> a	b	c
>> 1	2	3
>> 3	3	4
>> 2	4	5
>> 1	3	4
>> etc.
>> 
>> 
>> I want to select all rows where b=3 and then order by a.
>> 
>> To subset is easy:  x[x$b==3,]
>> To order is easy: x[order(x$a),]
>> 
>> Is there a way to do both in a single efficient statement?
>> 
>> Thanks,
>> 
>> 
>> 
>> --
>> Noah Silverman, M.S., C.Phil
>> UCLA Department of Statistics
>> 8117 Math Sciences Building
>> Los Angeles, CA 90095
>> 
>> 
>> 
>> 
>> 	[[alternative HTML version deleted]]
>> 
>> 
>> 
>> ______________________________________________
>> 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.
>> 

______________________________________________
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