【问题标题】:table() function in r - is there a better way with e.g., dplyr?r 中的 table() 函数 - 有没有更好的方法,例如 dplyr?
【发布时间】:2021-03-14 08:08:27
【问题描述】:

我正在尝试创建一个基本表,其中分类变量 (Relationship_type) 的频率由另一个变量 (Country) 分组,最好使用 dplyr 库(或任何其他比 table() 更容易导出为 .csv 文件的东西)。

head(d)
Country                        Relationship_type
1 Algeria                                      2
2 Bulgaria                                     1
3 USA                                          2
4 Algeria                                      3
5 Germany                                      2
6 USA                                          1

我希望它看起来像基本 table(d$Country, d$Relationship_type) 函数的输出:

                    2   3   4
  Algeria         141  47 137
  Australia       128  27 103
  Austria          97   5  17
  Belgium         172  16  71
  Brazil          104   6  70
  CHILE            54   4  46

尝试了几种tally()、group_by、count()等的组合,但无法弄清楚。

你能帮忙吗?

一切顺利, R_初学者

【问题讨论】:

    标签: r dplyr count


    【解决方案1】:

    另一种方法是使用tables::tabular(),如下所示。

    textData <- "id Country                        Relationship_type
    1 Algeria                                      2
    2 Bulgaria                                     1
    3 USA                                          2
    4 Algeria                                      3
    5 Germany                                      2
    6 USA                                          1
    7 Algeria                                      1
    8 Bulgaria                                     3
    9 USA                                          2
    10 Algeria                                     2
    11 Germany                                     1
    12 USA                                         3"
    
    df <- read.table(text=textData,header=TRUE)
    library(tables)
    tabular(Factor(Country) ~ Factor(Relationship_type),data=df)
    

    ...和输出:

              Relationship_type    
     Country  1                 2 3
     Algeria  1                 2 1
     Bulgaria 1                 0 1
     Germany  1                 1 0
     USA      1                 2 1
    

    还有一种方法是将table() 的输出重铸为数据框,并使用tidyr::pivot_wider() 将其旋转得更宽。

    # another approach: recast table output as data.frame
    tableData <- data.frame(table(df$Country,df$Relationship_type))
    library(dplyr)
    library(tidyr)
    tableData %>% 
         pivot_wider(id_cols = Var1,
                     names_from = Var2,
                     values_from = Freq)
    

    ...和输出:

    > tableData %>% 
    +      pivot_wider(id_cols = Var1,
    +                  names_from = Var2,
    +                  values_from = Freq)
    # A tibble: 4 x 4
      Var1       `1`   `2`   `3`
      <fct>    <int> <int> <int>
    1 Algeria      1     2     1
    2 Bulgaria     1     0     1
    3 Germany      1     1     0
    4 USA          1     2     1
    

    如果我们在管道中添加dplyr::rename(),我们可以将Var1 列重命名为Country

    tableData %>% 
         pivot_wider(id_cols = Var1,
                     names_from = Var2,
                     values_from = Freq) %>%
         rename(Country = Var1)
    

    像往常一样,R 中有很多方法可以完成这项任务。根据所需输出是 CSV 文件的原因,有多种方法可以满足要求。如果最终目标是创建演示质量表,那么值得看一下创建演示质量表的软件包摘要:How gt fits with other packages that create display tables

    【讨论】:

    • 感谢您的回答!我一定会检查 gt :) 表格在每个 Relationship_type 列中只给了我一个,因为我有来自给定国家的众多参与者,如我在第一篇文章中添加的 table() 输出所示(而不是二进制是或否的情况),所以我认为它无论如何都不会改善当前的情况。哦!没有注意到您的支点建议。这就是我一直在寻找的,谢谢!
    • @chillos - 感谢您的反馈。我已经更新了我的答案。如果您删除 *(N=1) tabular() 将自动计算观察次数。我还添加了测试数据以证明更新后的解决方案有效,正如您从输出中看到的那样,两个解决方案现在在表格单元格中总共有 12 个计数,其中一些计数大于 1。
    【解决方案2】:

    你确实可以使用count:

    d %>% count(Country, Relationship_type)
    

    它并不比table 好,但它至少节省了$ 的使用。

    【讨论】:

    • 感谢您的回答!不幸的是,它不等同于 table() 的输出 :( 但我想我设法通过冗长而垃圾的行来解决这个问题,如下所示:~test % count(Country, Relationship_type) test$Country
    猜你喜欢
    • 1970-01-01
    • 2011-12-01
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多