[R] Subset and order at the same time?

Noah Silverman noahsilverman at ucla.edu
Sat May 12 20:04:26 CEST 2012


Is there a way to order data and subset it at the same time??



I want to sort all the members of group A by their values in column 3. (I'll then do the same for each subsequent group.)  This could be done in a loop building up another vector, but I like to avoid loops in R.
-------------------------------------
a <- temp[temp$group=="A",]
a <- a[order(a[,3]),]
temp[temp$group=="A",] <- a
------------------------------------------

Iid like to do this in a single step for each group.  However, I can't figure out how to order and subset at the same time.

This *does not work* but should illustrate what I'm trying to do

temp[temp$group=="A",] <- temp[ temp$group=="A" & order(temp[temp$group=="A",3]) , ]


Suggestions?

--
Noah Silverman
UCLA Department of Statistics
8208 Math Sciences Building
Los Angeles, CA 90095



More information about the R-help mailing list