Quantcast

reorder data frame columns (not manually)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

reorder data frame columns (not manually)

glaporta
Hi,
is it possible to reorder (not manually) the data frame columns on the basis of their value?
Thanks,
Gianandrea

xxx <- data.frame(matrix(runif(8), ncol=8))
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: reorder data frame columns (not manually)

Manabu Sakamoto-2
I don't think this is a data.table specific question, but you can use order:

dt[order(V), ] for data.table object...

or

df[order(df$V), ] for data.frame object



On 17 March 2017 at 10:33, glaporta <[hidden email]> wrote:
Hi,
is it possible to reorder (not manually) the data frame columns on the basis
of their value?
Thanks,
Gianandrea

xxx <- data.frame(matrix(runif(8), ncol=8))



--
View this message in context: http://r.789695.n4.nabble.com/reorder-data-frame-columns-not-manually-tp4729802.html
Sent from the datatable-help mailing list archive at Nabble.com.
_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help



--
Manabu Sakamoto, PhD

_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: reorder data frame columns (not manually)

glaporta
Hi, thank you for your quick reply.
I don't want to order the values on a specific column. For a single row data frame, I want to rearrange the position of the columns: from X1, X2, X3, ... to X2, X3, X1 on the basis of their values.
Cheers
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: reorder data frame columns (not manually)

glaporta
> If you've only got a single row like in your example, can you not transpose it first and reorder the column on the transposed data.frame object?

Thank you Manabu,
I can't transpose for my real project, but the solution is xxx[,order(xxx)]
Loading...