如何根据因子水平排列data.frame?

时间:2020-06-11 10:35:25

标签: r

有data.frame df,我想根据因子级别按索引列进行排列(排序)。 结果为“希望的data.frame”。任何人都可以帮助?谢谢!

#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

2 个答案:

答案 0 :(得分:1)

喜欢吗?

import getHipHopLessons from "./hipHopLessons";

 export async function getLesson(lessonId) {
    const lessons = getHipHopLessons();
    return lessons.find((lesson) => lesson._id === lessonId);
 }

数据

arrange(df, match(df$index, levels(df$index)))

  index amount
1     a     76
2     b     10
3     e     60

答案 1 :(得分:0)

您可以使用order

df[order(df$index), ]

#  index amount
#2     a     76
#1     b     10
#3     e     60