[R] Subset and order at the same time?

Joshua Wiley jwiley.psych at gmail.com
Sun May 13 01:14:30 CEST 2012


Hi Noah,

I think it is hard to say what is best without your real example.  Is
the goal elegance or speed?  I have not tried it, but if you are
ordering say 9/10 groups, I would think you are better off calling
order once, even though you will not use it for one group.

I also think if you are ordering multiple groups, you should take
advantage of that and do it all at once rather than in a series of one
liners.

## A
sleep$index <- order(sleep$extra)
sleep[sleep$group == 1, ] <- with(subset(sleep, group == 1), sleep[index, ])

## A "one line"
sleep$index <- order(sleep$extra); sleep[sleep$group == 1, ] <-
with(subset(sleep, group == 1), sleep[index, ])

## B
sleep[sleep$group == 1, ] <- with(subset(sleep, group == 1),
sleep[order(extra), ])

I agree with Neal though, the one line solutions are probably
something you can do but probably should not do.  At an extreme:

a <- b <- 10; dat <- local(data.frame(first = b, second = a <- 0 -> b,
third = a <- rnorm(10, mean = a), fourth = a / 2, fifth = a <-
rnorm(10, mean = a <- b), sixth = a * rnorm(10, b) -> a, seventh = b
<<- a^2, eigth = {rm(b); 1:10}, ninth = a * b, tenth = b <<- a -> b,
eleventh = {rm(a); b <- 10:1}, twelth = a, thirteenth = local(a <<- b
-> a)))

Cheers,

Josh

On Sat, May 12, 2012 at 1:04 PM, Neal Fultz <nfultz at gmail.com> wrote:
> 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.
>>
>
> ______________________________________________
> 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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group
University of California, Los Angeles
https://joshuawiley.com/



More information about the R-help mailing list