how to order alphabetically rows of a data frame?

This question already has an answer here:

  • How to sort a dataframe by multiple column(s)? 16 answers

  • One solution with dplyr :

    library(dplyr)
    df %>%
      group_by(x) %>%
      arrange(c)
    

    Or as @Akrun mentions in the comments below just

    df %>%
      arrange(x,c)
    

    if you are not interested in grouping. Depends on what you want.

    Output:

    Source: local data frame [5 x 2]
    Groups: x
    
      x c
    1 2 A
    2 2 D
    3 3 B
    4 3 C
    5 5 E
    

    There is another solution in base R but it will only work if your x column is ordered as is, or if you don't mind changing the order it has:

    > df[order(df$x, df$c), , drop = FALSE]
      x c
    2 2 A
    1 2 D
    4 3 B
    3 3 C
    5 5 E
    
    链接地址: http://www.djcxy.com/p/70848.html

    上一篇: 按r中的列排序数据帧

    下一篇: 如何按字母顺序排序数据帧?