[R] sort by decreasing columns?

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Sun Dec 14 06:53:09 CET 2014


In general, different rows would sort in different orders. Your example does not illustrate how you would want to address such a problem. Here I assume you are only interested in sorting by the values in the first row.

dta <- data.frame(a=c(2,3), b=c(3,5), c=c(8,9))
dta[,order(unlist(dta[1,]),decreasing=TRUE)]

Note that data frames don't usually contain data that one would want to manipulate this way... this type of thing would normally be associated with matrices, which always have all elements of a single type.

Please follow the instructions in the footer of this email next time... in particular post using plain text because HTML does not always survive the trip to us intact.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On December 13, 2014 4:13:28 PM PST, Nia Gupta via R-help <r-help at r-project.org> wrote:
>Hello, 
>
>I have a data frame that looks like this:
>           a      b    c
>1        2       3    8
>2        3       5    9
>I was wondering if it was possible to reorder the columns by decreasing
>values so the new data frame would look like this:
>           c      b    a
>1        8      3    2
>2        9      5   3
>
>Thank you
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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