【问题标题】:R Arrange rows by a specific value within a variableR按变量中的特定值排列行
【发布时间】:2019-11-25 19:44:38
【问题描述】:

我想在 tibble 中排列行,以便其中包含“Gas”的值位于 tibble 的底部。

这是我的数据:

library(dplyr)

df1 <- tibble(
  col1 = c("ZBottom Gas","Almost Bottom Gas","Top","Bottom Gas", "Top"),
  col2 = c(5, 7, 4, 8,6))

这是我希望数据的样子:

df1 <- tibble(
  col1 = c("Top", "Top", "ZBottom Gas","Almost Bottom Gas","Bottom Gas"),
  col2 = c(4, 6, 5, 7, 8))

我知道我可以将一个新变量分配给任何气体,其值为“2”,然后将其他所有内容分配给“1”,然后像这样使用:

df2 <- tibble(
  col1 = c("ZBottom Gas","Almost Bottom Gas","Top","Bottom Gas", "Top"),
  col2 = c(5, 7, 4, 8,6),
  arrange = c(2,2,1,2,1))

df2 %>% 
  arrange(arrange) -> df3

这很好,但我只是想知道是否有更简单的方法来做到这一点?

谢谢

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    我们可以使用str_detect来检测"Gas"的存在,并在arrange中使用

    library(dplyr)
    library(stringr)
    
    df1 %>% arrange(str_detect(col1, 'Gas'))
    
    #  col1               col2
    #  <chr>             <dbl>
    #1 Top                   4
    #2 Top                   6
    #3 ZBottom Gas           5
    #4 Almost Bottom Gas     7
    #5 Bottom Gas            8
    

    在基础 R 中,可以使用 ordergrepl 来完成。

    df1[order(grepl('Gas', df1$col1)), ]
    

    【讨论】:

    • 太完美了!从来没有听说过 grepl,不过它会非常有用
    【解决方案2】:

    regexpr() 也吃零食。

    df1[order(regexpr("Gas", df1$col1)), ]
    # # A tibble: 5 x 2
    #   col1               col2
    #   <chr>             <dbl>
    # 1 Top                   4
    # 2 Top                   6
    # 3 Bottom Gas            8
    # 4 ZBottom Gas           5
    # 5 Almost Bottom Gas     7
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-12
      相关资源
      最近更新 更多