[R] Subset and order at the same time?

Neal Fultz nfultz at gmail.com
Sat May 12 22:04:21 CEST 2012


can be done in one line, but it is annoying and ugly, so you probably
shouldn't be doing it that way:

>sleep[sleep$group == 1,] <-sleep[sleep$group == 1,][order(sleep[sleep$group == 1,1]),]
>sleep
   extra group ID
1   -1.6     1  2
2   -1.2     1  4
3   -0.2     1  3
4   -0.1     1  5
5    0.0     1  9
6    0.7     1  1
7    0.8     1  8
8    2.0     1 10
9    3.4     1  6
10   3.7     1  7
11   1.9     2  1
12   0.8     2  2
13   1.1     2  3
14   0.1     2  4
15  -0.1     2  5
16   4.4     2  6
17   5.5     2  7
18   1.6     2  8
19   4.6     2  9
20   3.4     2 10


On 5/12/12, jim holtman <jholtman at gmail.com> wrote:
> Just write a function so that you have a "one-liner" in your script.
> It will probably be a lot simpler than trying to type some convoluted
> one-liner.
>
> On Sat, May 12, 2012 at 2:58 PM, Noah Silverman <noahsilverman at ucla.edu>
> wrote:
>> Bernard,
>>
>> Thanks, but I can't take that shortcut.
>>
>> The data is an xts object, and I may not want to order every group.  So, I
>> need a way to just order one group at a time.
>>
>> Thoughts?
>>
>> --
>> Noah Silverman
>> UCLA Department of Statistics
>> 8208 Math Sciences Building
>> Los Angeles, CA 90095
>>
>> On May 12, 2012, at 11:38 AM, Berend Hasselman wrote:
>>
>>>
>>> On 12-05-2012, at 20:04, Noah Silverman wrote:
>>>
>>>> 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?
>>>
>>> set.seed(413)
>>>
>>> temp <- data.frame(group=rep(c("A","B","C"), rep=5),
>>> tt=1:15,val=round(runif(15),2), stringsAsFactors=FALSE)
>>> idx <- order(temp$group,temp$val)
>>> # or   idx <- order(temp[,1],temp[,3])
>>> idx
>>> z2 <- temp[idx,]
>>> rownames(z2) <- NULL
>>> z2
>>>
>>> Possible NA's etc. not taken into account.
>>>
>>> Berend
>>
>>
>>        [[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.
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
> ______________________________________________
> 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