代码之家  ›  专栏  ›  技术社区  ›  Nick Camarda

如果为FALSE,则递增列,如果为TRUE,则停止递增(tidyverse、dplyr、R)

  •  0
  • Nick Camarda  · 技术社区  · 3 年前

    我有一张这样的桌子:

    data <- structure(list(group = c(0L, 0L, 1L, 2L), id = c("1", "2", "3", 
    "4"), m = c("ac1", "ac1", "ac1", "me0"), together = c(FALSE, 
    FALSE, TRUE, TRUE)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
    -4L))
    

    我想做个专栏 group together == FALSE together == TRUE .

    这是我想要的输出:

    # A tibble: 4 x 4
      id    m     together group
      <chr> <chr> <lgl>    <dbl>
    1 1     ac1   FALSE        0
    2 2     ac1   FALSE        1
    3 3     ac1   TRUE         2
    4 4     me0   TRUE         2
    

    我已经尝试过类似的解决方案 R increment by 1 for every change in value column and restart the counter 它并没有给我想要的东西。。

    # A tibble: 4 x 4
    # Groups:   group [3]
      id    m     together group
      <chr> <chr> <lgl>    <int>
    1 1     ac1   FALSE        0
    2 2     ac1   FALSE        0
    3 3     ac1   TRUE         1
    4 4     me0   TRUE         2
    

    在这种情况下,我希望组读0,1,2,2。有什么想法吗?非常感谢。

    1 回复  |  直到 3 年前
        1
  •  2
  •   DaveArmstrong    3 年前

    这里有一个简洁的解决方案:

    library(dplyr)
    #> 
    #> Attaching package: 'dplyr'
    #> The following objects are masked from 'package:stats':
    #> 
    #>     filter, lag
    #> The following objects are masked from 'package:base':
    #> 
    #>     intersect, setdiff, setequal, union
    data <- structure(list(id = c("1", "2", "3", "4"), 
                           m = c("ac1", "ac1", "ac1", "me0"), 
                           together = c(FALSE, FALSE, TRUE, TRUE)), 
                      class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -4L))
    data <- data %>% mutate(
      group = cumsum(c(0, na.omit(lag(!data$together)))))
    data
    #> # A tibble: 4 × 4
    #>   id    m     together group
    #>   <chr> <chr> <lgl>    <dbl>
    #> 1 1     ac1   FALSE        0
    #> 2 2     ac1   FALSE        1
    #> 3 3     ac1   TRUE         2
    #> 4 4     me0   TRUE         2
    

    于2022年4月24日由 reprex package (v2.0.1)

        2
  •  0
  •   Onyambu    3 年前

    另一种解决方法如下所示

    data %>%
           mutate(grp2 = c(0, cumsum(!na.omit(together * lag(together)))))
        # A tibble: 4 x 5
          group id    m     together  grp2
          <int> <chr> <chr> <lgl>    <dbl>
        1     0 1     ac1   FALSE        0
        2     0 2     ac1   FALSE        1
        3     1 3     ac1   TRUE         2
        4     2 4     me0   TRUE         2
    

    使用注释中给出的数据并运行相同的代码:

    data1 <- data.frame(together=c(FALSE, FALSE, TRUE, TRUE, FALSE, TRUE))
    data1 %>%
       mutate(grp2 = c(0, cumsum(!na.omit(together * lag(together)))))
      together grp2
    1    FALSE    0
    2    FALSE    1
    3     TRUE    2
    4     TRUE    2
    5    FALSE    3
    6     TRUE    4
    
    推荐文章