【问题标题】:Sort dataframe in R (based on column values)在 R 中对数据框进行排序(基于列值)
【发布时间】:2019-04-27 14:06:58
【问题描述】:

我想根据列中的值(字符)对 R 中的数据框进行半反向排序。

我有以下示例数据集:

# Sample data
df <- read.table(text="id value
                 cx-01    1
                 cx-01    2
                 cx-02    1
                 cx-02    2
                 cx-02    3
                 cx-03    1
                 cx-03    2 
                 px-01    1
                 px-01    2
                 px-02    1
                 px-02    2
                 px-02    3
                 px-03    1
                 px-03    2
                 rx-01    1
                 rx-01    2
                 rx-02    1
                 rx-02    2
                 rx-02    3
                 rx-03    1
                 rx-03    2", header=TRUE)

预期输出:

      id value
1  cx-03     2
2  cx-03     1
3  cx-02     3
4  cx-02     2
5  cx-02     1
6  cx-01     2
7  cx-01     1
8  rx-03     2
9  rx-03     1
10 rx-02     3
11 rx-02     2
12 rx-02     1
13 rx-01     2
14 rx-01     1
15 px-03     2
16 px-03     1
17 px-02     3
18 px-02     2
19 px-02     1
20 px-01     2
21 px-01     1

我尝试使用 base R 的 order() 函数,但遗憾的是没有成功。此外,我尝试使用 plyr 包的排列功能,但是,我没有设法按需要对数据进行排序。

是否可以根据自己提供的顺序(而不是按字母顺序)对第一列中的标签进行排序?

【问题讨论】:

    标签: r sorting dataframe


    【解决方案1】:

    使用来自基础 R 的 with() 和 order()

    # sample data
    df <- read.table(text="id value
                     cx-01    1
                     cx-01    2
                     cx-02    1
                     cx-02    2
                     cx-02    3
                     cx-03    1
                     cx-03    2 
                     px-01    1
                     px-01    2
                     px-02    1
                     px-02    2
                     px-02    3
                     px-03    1
                     px-03    2
                     rx-01    1
                     rx-01    2
                     rx-02    1
                     rx-02    2
                     rx-02    3
                     rx-03    1
                     rx-03    2", header=TRUE, stringsAsFactors=F)
    
    # create another data frame with variables to order on
    col.ord <- data.frame(t(sapply(strsplit(df$id, "-"), print)), df$value, stringsAsFactors = F)
    
    # reorder data frame
    df[with(col.ord, order(X1, -as.integer(X2), -df.value)), ]
    #>       id value
    #> 7  cx-03     2
    #> 6  cx-03     1
    #> 5  cx-02     3
    #> 4  cx-02     2
    #> 3  cx-02     1
    #> 2  cx-01     2
    #> 1  cx-01     1
    #> 14 px-03     2
    #> 13 px-03     1
    #> 12 px-02     3
    #> 11 px-02     2
    #> 10 px-02     1
    #> 9  px-01     2
    #> 8  px-01     1
    #> 21 rx-03     2
    #> 20 rx-03     1
    #> 19 rx-02     3
    #> 18 rx-02     2
    #> 17 rx-02     1
    #> 16 rx-01     2
    #> 15 rx-01     1
    

    由reprex package (v0.2.1) 于 2019 年 4 月 27 日创建

    【讨论】:

      【解决方案2】:

      我们可以将arrange分别放在'id'的数字和字母部分,同时将'value'按descending顺序排列。字母部分似乎是自定义顺序,因此要么转换为 factor 并指定 levels,要么使用 match 和 vector,其顺序与预期以该顺序获取索引的顺序相同

      library(tidyverse)
      df %>%  
         arrange(match(str_remove(id, "-\\d+"), c("cx", "rx", "px")), 
                readr::parse_number(as.character(id)), desc(value))
      #      id value
      #1  cx-03     2
      #2  cx-03     1
      #3  cx-02     3
      #4  cx-02     2
      #5  cx-02     1
      #6  cx-01     2
      #7  cx-01     1
      #8  rx-03     2
      #9  rx-03     1
      #10 rx-02     3
      #11 rx-02     2
      #12 rx-02     1
      #13 rx-01     2
      #14 rx-01     1
      #15 px-03     2
      #16 px-03     1
      #17 px-02     3
      #18 px-02     2
      #19 px-02     1
      #20 px-01     2
      #21 px-01     1
      

      【讨论】:

        猜你喜欢
        • 2021-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-04
        • 2018-10-17
        • 2020-06-19
        • 1970-01-01
        • 2019-03-31
        相关资源
        最近更新 更多