【问题标题】:Binary Variables Combinations Analysis in RR中的二元变量组合分析
【发布时间】:2021-11-30 03:14:26
【问题描述】:

我有一个数据集,其中有很多二进制变量。为了便于说明,这里是一个只有 4 个变量的较小版本:

set.seed(5)
my_data<-data.frame("Slept Well"=sample(c(0,1),10,TRUE),
                    "Had Breakfast"=sample(c(0,1),10,TRUE),
                    "Worked out"=sample(c(0,1),10,TRUE),
                    "Meditated"=sample(c(0,1),10,TRUE))

在上面,每一行对应一个观察。我有兴趣分析变量的每个独特组合的频率。例如,有多少观察表明他们都睡得很好,并且冥想,但没有吃早餐或锻炼?

我希望能够将唯一组合从最频繁出现到最不频繁出现进行排名。编写代码的最佳方法是什么?

【问题讨论】:

    标签: r dataframe binary combinations frequency


    【解决方案1】:

    您可以使用aggregate

    x <- aggregate(list(n=rep(1, nrow(my_data))), my_data, length)
    #x <- aggregate(list(n=my_data[,1]), my_data, length) #Alternative
    x[order(-x$n),]
    #  Slept.Well Had.Breakfast Worked.out Meditated n
    #4          0             1          1         0 2
    #1          0             0          0         0 1
    #2          1             1          0         0 1
    #3          0             0          1         0 1
    #5          0             0          0         1 1
    #6          1             0          0         1 1
    #7          0             1          0         1 1
    #8          0             0          1         1 1
    #9          0             1          1         1 1
    

    【讨论】:

      【解决方案2】:

      dplyr 解决方案怎么样:

      library(dplyr)
         
          my_data %>%
        # group it
        group_by_all() %>%
        # frequencies
        summarise(freq = n()) %>%
        # order decreasing
        arrange(-freq)
      
      # A tibble: 9 x 5
        Slept.Well Had.Breakfast Worked.out Meditated  freq
        <chr>      <chr>         <chr>      <chr>     <int>
      1 0          1             1          0             2
      2 0          0             0          0             1
      3 0          0             0          1             1
      4 0          0             1          0             1
      5 0          0             1          1             1
      6 0          1             0          1             1
      7 0          1             1          1             1
      8 1          0             0          1             1
      9 1          1             0          0             1
      

      或者data.table:

      res <- setorder(data.table(my_data)[,"."(freq = .N), by = names(my_data)],-freq)
      res
         Slept.Well Had.Breakfast Worked.out Meditated freq
      1:          0             1          1         0    2
      2:          1             0          0         1    1
      3:          0             0          1         0    1
      4:          0             0          0         0    1
      5:          0             1          0         1    1
      6:          0             1          1         1    1
      7:          0             0          1         1    1
      8:          0             0          0         1    1
      9:          1             1          0         0    1
      

      【讨论】:

        猜你喜欢
        • 2016-11-25
        • 1970-01-01
        • 1970-01-01
        • 2022-01-01
        • 2021-06-13
        • 2014-06-14
        • 1970-01-01
        • 2018-07-18
        相关资源
        最近更新 更多