【问题标题】:How do I label the group for consecutive pattern in R?如何在 R 中为连续模式标记组?
【发布时间】:2019-12-18 15:32:39
【问题描述】:

我正在尝试为每个组添加标签。这是数据集。

   group
1    p01
2    p01
3    p01
4    p01
5    p02
6    p01
7    p01
8    p01
9    p02
10   p02
11   p01
12   p01

结构(列表(组=结构(c(1L,1L,1L,1L,2L,1L,1L,1L, 2L, 2L, 1L, 1L), .Label = c("p01", "p02"), class= "因子")), class= "data.frame", row.names = c(NA, -12L))

这是预期的表格。在 p01 的情况下,预期的列是 1-4 中的 1,然后是 6-8 中的 2,11-12 中的 3 用于考虑连续模式。

   group new_group
1    p01         1
2    p01         1
3    p01         1
4    p01         1
5    p02         1
6    p01         2
7    p01         2
8    p01         2
9    p02         2
10   p02         2
11   p01         3
12   p01         3

如何在 r 中使用 dplyr?

【问题讨论】:

  • rle 在这里可能有用。
  • 我会考虑的。谢谢。

标签: r label


【解决方案1】:

另一种可能性:

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

df <- structure(list(group = structure(c(1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L), .Label = c("p01", "p02"), class = "factor")), class = "data.frame", row.names = c(NA, -12L))

df %>%
  mutate(new_group = with(rle(as.integer(group)), rep(seq_along(lengths), lengths))) %>%
  group_by(group) %>%
  transmute(new_group = as.integer(as.factor(new_group))) %>%
  ungroup()
#> # A tibble: 12 x 2
#>    group new_group
#>    <fct>     <int>
#>  1 p01           1
#>  2 p01           1
#>  3 p01           1
#>  4 p01           1
#>  5 p02           1
#>  6 p01           2
#>  7 p01           2
#>  8 p01           2
#>  9 p02           2
#> 10 p02           2
#> 11 p01           3
#> 12 p01           3

reprex package (v0.3.0) 于 2019 年 8 月 12 日创建

【讨论】:

    【解决方案2】:

    是这个吗?

    x %>%
      mutate(new_group = cumsum(group == "p01" & lag(group != "p01", default = TRUE)))
    #    group new_group
    # 1    p01         1
    # 2    p01         1
    # 3    p01         1
    # 4    p01         1
    # 5    p02         1
    # 6    p01         2
    # 7    p01         2
    # 8    p01         2
    # 9    p02         2
    # 10   p02         2
    # 11   p01         3
    # 12   p01         3
    

    【讨论】:

    • 根据您提供的示例数据并且没有解释任何新的分组逻辑,我认为我在重现您的预期输出方面做得很好。请做两件事:(1)提供更多变数的数据样本; (2) 讨论这样做时必须考虑的逻辑。
    • 对不起,我会这样做的。真的很对不起你。
    • 不需要道歉,真的,只需尝试改进 MWE 以更好地代表您的需求。
    【解决方案3】:

    我们可以使用row_number() 创建一个列,并为每个group 增加计数器,只要行的差异大于1。

    library(dplyr)
    
    df %>%
      mutate(row = row_number()) %>%
      group_by(group) %>%
      mutate(new_group = cumsum(row - lag(row, default = first(row)) > 1) + 1) %>%
      select(-row)
    
    #   group new_group
    #   <fct>     <dbl>
    # 1 p01           1
    # 2 p01           1
    # 3 p01           1
    # 4 p01           1
    # 5 p02           1
    # 6 p01           2
    # 7 p01           2
    # 8 p01           2
    # 9 p02           2
    #10 p02           2
    #11 p01           3
    #12 p01           3
    

    或者使用diff更短一点

    df %>%
      mutate(row = row_number()) %>%
      group_by(group) %>%
      mutate(new_group = cumsum(c(TRUE, diff(row) > 1))) %>%
      select(-row)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 2019-12-05
      • 1970-01-01
      相关资源
      最近更新 更多