Reputation: 2411
there is data.frame df, i want to arrange (sort) it by index column according the factor levels. the result as "the wished data.frame". anyone can help ? thanks!
#create data frame
df<-data.frame(index=c("b","a","e"),amount=c(10,76,60))
df$index<-factor(df$index,levels=c("a","b","e"))
# current df
index amount
1 b 10
2 a 76
3 e 60
# the wished data.frame
index amount
1 a 76
2 b 10
3 e 60
Upvotes: 0
Views: 1749
Reputation: 388807
You can use order
:
df[order(df$index), ]
# index amount
#2 a 76
#1 b 10
#3 e 60
Upvotes: 2
Reputation: 3876
Like this?
arrange(df, match(df$index, levels(df$index)))
index amount
1 a 76
2 b 10
3 e 60
Data
df<-data.frame(index=c("b","a","e"),amount=c(10,76,60))
df$index<-factor(df$index,levels=c("b","e","a"))
Upvotes: 4