Sorting data frames by columns in r

r

Solution

`Sp1` should not be quoted when you are using `with`. This would always just return `1` and thus just return your first row. Try this instead:

> df[order(df$Sp1),] 
    Sp1 Sp2
A03   2   1
A02   4   2
A01   7   6
> df[with(df, order(Sp1)), ]
    Sp1 Sp2
A03   2   1
A02   4   2
A01   7   6

Problem

Possible Duplicate: How to sort a dataframe by column(s) in R I am trying to sort a data.frame by several columns ``` df<-data.frame("Sp1"=c(7,4,2),"Sp2"=c(6,2,1)) row.names(df)<-c("A01","A02","A03") Sp1 Sp2 A01 7 6 A02 4 2 A03 2 1 #I am using df[with(df, order("Sp1"))] ``` however this does nothing. Any ideas why? Thanks

Original source

Related problems