【问题标题】:counting how many times a variable changes score by group计算变量按组更改分数的次数
【发布时间】:2019-11-15 12:59:46
【问题描述】:

我有一个非常基本的问题,我有点纠结, 我有一个面板大面板数据集,看起来像这样:

df <- data.frame(id= c(1,1,1,2,2,2,3,3,3,4,4,4), time=c(1,2,3,1,2,3,1,2,3,1,2,3), x = c(0,0,0,0,1,1,0,0,1,0,1,2))

我想找到一种紧凑的方法来计算我的 x 变量为每个 id 更改了多少次。 最终的数据集应该是这样的

df <- data.frame(id= c(1,1,1,2,2,2,3,3,3,4,4,4), time=c(1,2,3,1,2,3,1,2,3,1,2,3), x = c(0,0,0,0,1,1,0,0,1,0,1,2),count= c(0,0,0,1,1,1,1,1,1,2,2,2))

理想情况下我想使用 dplyr

我在想我应该做类似的事情

library(dplyr)
df <- df %>% group_by(id) %>% mutate(count=)

但是我不确定如何完成它,因为我不知道我可以使用什么样的命令来计算分数的变化。

非常感谢您的帮助

【问题讨论】:

    标签: r count data-manipulation


    【解决方案1】:

    可以使用不等于0的x的滞后差之和:

    library(dplyr)
    
     df %>% 
       group_by(id) %>%
       mutate(count = sum(diff(x) != 0))
    
       id time x count
    1   1    1 0     0
    2   1    2 0     0
    3   1    3 0     0
    4   2    1 0     1
    5   2    2 1     1
    6   2    3 1     1
    7   3    1 0     1
    8   3    2 0     1
    9   3    3 1     1
    10  4    1 0     2
    11  4    2 1     2
    12  4    3 2     2
    

    【讨论】:

      【解决方案2】:

      使用dplyr,这是使用lag的一种方式

      library(dplyr)
      df %>%
        group_by(id) %>%
        mutate(count = length(unique(cumsum(x != lag(x, default = first(x))))) - 1)
      
      
      #     id  time     x count
      #   <dbl> <dbl> <dbl> <dbl>
      # 1     1     1     0     0
      # 2     1     2     0     0
      # 3     1     3     0     0
      # 4     2     1     0     1
      # 5     2     2     1     1
      # 6     2     3     1     1
      # 7     3     1     0     1
      # 8     3     2     0     1
      # 9     3     3     1     1
      #10     4     1     0     2
      #11     4     2     1     2
      #12     4     3     2     2
      

      data.table::rleid 会变得更短

      df %>%
        group_by(id) %>%
        mutate(count = length(unique(data.table::rleid(x))) - 1)
      

      【讨论】:

        【解决方案3】:

        我们可以使用rlen_distinct

        library(dplyr)
        df %>%
           group_by(id) %>% 
           mutate(count = n_distinct(rle(x)$values)-1)
        # A tibble: 12 x 4
        # Groups:   id [4]
        #      id  time     x count
        #   <dbl> <dbl> <dbl> <dbl>
        # 1     1     1     0     0
        # 2     1     2     0     0
        # 3     1     3     0     0
        # 4     2     1     0     1
        # 5     2     2     1     1
        # 6     2     3     1     1
        # 7     3     1     0     1
        # 8     3     2     0     1
        # 9     3     3     1     1
        #10     4     1     0     2
        #11     4     2     1     2
        #12     4     3     2     2
        

        data.table

        library(data.table)
        setDT(df)[, count := uniqueN(rleid(x)) - 1, id]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-08-31
          • 1970-01-01
          • 2022-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-10
          相关资源
          最近更新 更多