【问题标题】:Counting the average of duplicates per id in R计算 R 中每个 id 重复的平均值
【发布时间】:2021-07-30 09:03:37
【问题描述】:

我的数据如下所示:

id date
1 a
1 a
1 b
1 c
1 c
1 c
2 z
2 z
2 e
2 x

我想计算每个 id 重复的平均值,即对于 id=1,我们有 2a 1b 3c 我希望输出为 2。 结果应该是这样的:

id mean
1 2
2 1.333

【问题讨论】:

    标签: r data-science


    【解决方案1】:

    您可以使用mean(table(date)) 来获取计数的平均值,将其应用于每个id 值。

    使用 dplyr -

    library(dplyr)
    
    df %>%
      group_by(id) %>%
      summarise(mean = mean(table(date)))
    
    #     id  mean
    #  <int> <dbl>
    #1     1  2   
    #2     2  1.33
    

    或者使用基础 R aggregate

    aggregate(date~id, df, function(x) mean(table(x)))
    

    【讨论】:

      【解决方案2】:

      你可以试试tidyverse

      library(tidyverse)
      d %>% 
        group_by(id) %>% 
        count(date) %>% 
        summarise(mean = mean(n))
      # A tibble: 2 x 2
           id  mean
        <int> <dbl>
      1     1  2   
      2     2  1.33
      

      使用baseR你可以试试

      foo <- function(x) mean(rle(x)$length)
      aggregate(d$date, by=list(d$id), foo)
      

      数据

      d <- read.table(text ="id   date
      1     a
      1     a
      1     b
      1     c
      1     c
      1     c
      2     a
      2     a
      2     e
      2     z", header=T)
      

      【讨论】:

        【解决方案3】:

        使用data.table

        library(data.table)
        # dt <- your_data_frame %>% as.data.table()  ## convert to table from frame
        dt[, .(N=.N), by = .(id,date)][, .(mean = mean(N)), by = id]
        

        【讨论】:

          【解决方案4】:

          另一个data.table 选项

          > setDT(df)[, .(Mean = .N / uniqueN(date)), id]
             id     Mean
          1:  1 2.000000
          2:  2 1.333333
          

          dcast(setDT(df), id ~ date, fill = NA)[, .(Mean = rowMeans(.SD, na.rm = TRUE)), id]
          

          给予

             id     Mean
          1:  1 2.000000
          2:  2 1.333333
          

          【讨论】:

            【解决方案5】:

            我们可以使用

            library(dplyr)
            df1 %>%
                 group_by(id) %>%
                 summarise(Mean = count(cur_data(), date) %>% 
                          pull(n) %>%
                           mean)
            

            【讨论】:

              【解决方案6】:

              使用base 包的table 方法:

              at<-table(a$id,a$date)
              apply(at,1,function(x) sum(x)/sum(x!=0))
              
              #       1        2 
              #2.000000 1.333333
              

              数据集:

              a = data.frame('id'=c(1,1,1,1,1,1,2,2,2,2),'date'=c('a','a','b','c','c','c','a','a','e','z'))
              

              【讨论】:

                【解决方案7】:

                这是一个免费的解决方案

                a = cbind(c(1,1,1,1,1,1,2,2,2,2),c('a','a','b','c','c','c','a','a','e','z'))
                b = matrix(ncol = 2)[-1,]
                for(i in unique(a[,1])){
                  
                  b=rbind(b,c(i,sum(table(a[a[,1]==i,2]))/length(table(a[a[,1]==i,2]))))
                }
                

                输出:

                    [,1] [,2]              
                [1,] "1"  "2"               
                [2,] "2"  "1.33333333333333"
                

                【讨论】:

                • 这很好,但应该使用data.frame() 而不是rbind() 以避免强制...
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2015-11-02
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-05-05
                相关资源
                最近更新 更多