【问题标题】:how to create tables in R with my specific pattern?如何使用我的特定模式在 R 中创建表?
【发布时间】:2020-09-29 19:57:35
【问题描述】:

我试图节省时间获取表格,如下例所示,但在 R 中,我无需自己应对并从 R 粘贴到 word

我想要转换成与上图类似的特定类型的数据就在这里:

test_data <-  structure(list(age_band = structure(c(3L, 3L, 3L, 2L, 1L, 2L, 
    2L, 1L, 1L, 3L), .Label = c("20-39", "40-59", "60+"), class = "factor"), 
        tested = c("positive", "positive", "showing symptoms", 
        "positive", "positive", "positive", "showing symptoms", "positive", 
        "positive", "showing symptoms"), comorbidities = structure(c(5L, 
        9L, 5L, 5L, 9L, 1L, 5L, 1L, 5L, 3L), .Label = c("asthma", 
        "diabetes_type_one", "diabetes_type_two", "heart_disease", 
        "hypertension", "kidney_disease", "liver_disease", "lung_condition", 
        "obesity"), class = "factor"), count = c(1L, 1L, 37L, 5L, 
        10L, 4L, 234L, 6L, 5L, 12L), percentage = c(50, 50, 45.7, 
        38.5, 35.7, 30.8, 30, 21.4, 17.9, 14.8)), row.names = c(NA, 
    -10L), groups = structure(list(age_band = structure(c(1L, 1L, 
    1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("20-39", "40-59", 
    "60+"), class = "factor"), tested = c("positive", "positive", 
    "positive", "positive", "positive", "showing symptoms", "positive", 
    "positive", "showing symptoms", "showing symptoms"), comorbidities = structure(c(1L, 
    5L, 9L, 1L, 5L, 5L, 5L, 9L, 3L, 5L), .Label = c("asthma", "diabetes_type_one", 
    "diabetes_type_two", "heart_disease", "hypertension", "kidney_disease", 
    "liver_disease", "lung_condition", "obesity"), class = "factor"), 
        .rows = structure(list(8L, 9L, 5L, 6L, 4L, 7L, 1L, 2L, 10L, 
            3L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", 
        "list"))), row.names = c(NA, 10L), class = c("tbl_df", "tbl", 
    "data.frame"), .drop = TRUE), class = c("grouped_df", "tbl_df", 
    "tbl", "data.frame"))

根据我的具体数据 - 我希望年龄范围 20-39; 40-59 and 60+ 就像上表中的 2015; 2016; 2017 和每个年龄组的正下方我希望 tested and showing symptoms 类别就像 writepass rate 按字母顺序排列的每个合并症订单 - asthma, cough, chills etc(应该以行的形式出现在侧面 - 就像 'Ontario RN university program collaborative partner, and language instruction'。我希望保留计数和百分比。

这在 R 中可能吗?我有 Kable,但它并没有按照我想要的方式进行。应该有类似的吧?

====== 更新问题====

如果 gt 库可以做到这一点,请告诉我?干杯!

【问题讨论】:

    标签: r datatables kable gt


    【解决方案1】:

    你可以使用tables package:

    library(tables)
    tables::tabular((comorbidities = factor(comorbidities)+1)*((n=1)+Percent("col"))~(age_band=factor(age_band))*(tested=factor(tested)), data=test_data)
    
                              tested                    tested                    tested                   
     comorbidities             positive showing symptoms positive showing symptoms positive showing symptoms
     asthma            n         1.00     0                1        0                0        0             
                       Percent  33.33   NaN               50        0                0        0             
     diabetes_type_two n         0.00     0                0        0                0        1             
                       Percent   0.00   NaN                0        0                0       50             
     hypertension      n         1.00     0                1        1                1        1             
                       Percent  33.33   NaN               50      100               50       50             
     obesity           n         1.00     0                0        0                1        0             
                       Percent  33.33   NaN                0        0               50        0             
     All               n         3.00     0                2        1                2        2             
                       Percent 100.00   NaN              100      100              100      100       
    

    然后您可以以您喜欢的格式导出结果,例如toHTML

    【讨论】:

    • RMarkdown 可以做到这一点吗?
    • 是的,无论是 Latex 还是 HMTL,请参见 linked vignette 的第 43 页。
    • 我得到一个错误:'百分比错误(“col”):找不到函数“百分比”'
    • 令人惊讶的是,我再次使用reprex 进行了测试,它可以在我的计算机上运行。你加载tables了吗?另外,我的代码使用Percent,所以我不完全理解Percentage 上的错误。
    • 因为在我的真实表格中它是百分比。嗯,我也加载了表库
    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 2014-03-27
    • 2016-04-14
    • 1970-01-01
    相关资源
    最近更新 更多