Sorting data frames by columns in 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
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
你也可以尝试在doBy
包中使用另一个bult-in函数:
# install.packages('doBy')
library(doBy)
orderBy(Sp1~Sp2, data=df)
Sp1 Sp2
A03 2 1
A02 4 2
A01 7 6
链接地址: http://www.djcxy.com/p/70850.html
上一篇: dplyr按反向字母顺序排列
下一篇: 按r中的列排序数据帧