[R] Ordering data by variable

Joshua Wiley jwiley.psych at gmail.com
Thu Sep 2 22:56:44 CEST 2010


Hi Marcio,

Is this what you want?

x <- c(2,6,8,8,1)
y <- c(1,6,3,5,4)
o <- order(x)

# If you want each vector order by x
x[o]
y[o]

You can also use sort(), but then each vector would be sorted by
itself, not both by x.

HTH,

Josh

On Thu, Sep 2, 2010 at 1:48 PM, Mestat <mestat at pop.com.br> wrote:
>
> Hi listers,
> I could order a data that like this:
> x<-c(2,6,8,8,1)
> y<-c(1,6,3,5,4)
> o<-order(x)
> frame<-rbind(x,y)[,o]
> But, I would like to know if there is a way to order my data without setting
> up a data frame. I would like to keep independent vectors x and y.
> Any suggestions?
> Thanks in advance,
> Marcio
> --
> View this message in context: http://r.789695.n4.nabble.com/Ordering-data-by-variable-tp2524754p2524754.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list