Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting data frames by columns in r [duplicate]

Tags:

r

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

like image 910
Elizabeth Avatar asked Jun 21 '26 03:06

Elizabeth


1 Answers

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
like image 163
A5C1D2H2I1M1N2O1R2T1 Avatar answered Jun 23 '26 17:06

A5C1D2H2I1M1N2O1R2T1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!