首页 > 解决方案 > 从具有不同变量名称的计数表创建比例函数

问题描述

问题:我有一个包含 100 个问题的调查。这些有可能有 5 种类型的响应,我已将它们分组并统计到单独的表格中(在列表中)。每个表都有不同数量的列,具有不同的变量名称。

样本数据:

tbl1 <-   tribble(~"stakeholder", ~"Question", ~"1-Little", ~"2",   ~"3",   ~"4-Much",  ~"Do not know/ Not applicable", ~"no_response",
        "SH_1", "QUESTION 2",   2,  1,  4,  8, 1,   1,
        "SH_2", "QUESTION 2",   2,  1,  4,  8, 1,   1,
        "SH_3", "QUESTION 2",   2,  1,  4,  8, 1,   1,
        "SH_4", "QUESTION 2",   2,  1,  4,  8, 1,   1,
)

tbl2 <- tribble(~"stakeholder", ~"Question", ~"1-Little",   ~"2",   ~"3",   ~"4-Much", ~"5-MuchMuch",   ~"Do not know/ Not applicable", ~"no_response",
                "SH_1", "QUESTION 2",   2,  1,  4,  8, 1,   1,2,
                "SH_2", "QUESTION 2",   2,  1,  4,  8, 1,   1,2,
                "SH_3", "QUESTION 2",   2,  1,  4,  8, 1,   1,2,
                "SH_4", "QUESTION 2",   2,  1,  4,  8, 1,   1,2
)

问题:如何根据总和创建比例计数? 我需要根据每个问题的回答总数创建比例表。

我通过基于分组变量的统计,从字符响应的样本表中创建了上述计数。我注意到我有 6 种不同的方式来分组和复制图形和表格(总共需要近 600 个!):

    tally_function <- function(tbl) {
  tbl %>% 
  gather(key = Question, value = Response,
         12:length(.)) %>% 
  group_by(stakeholder, Question, Response) %>% 
  tally %>% 
  spread(Response, n, fill = 0) %>% 
  select(stakeholder, Question, everything(), no_response = `<NA>`) %>% 
    arrange(Question)

}

我使用的前一个函数调用各个列名来生成总和,但这在这里不起作用,因为每个表中的列名都不同:

Prop_Function_Group1 <- function(tbl){
  tbl %>% 
    summarise(`Number of Responses (Count)` = sum(`1-Little` + `2`+`Do not know/ Not applicable`+
                                            `3`+`4-Much` + no_response, na.rm = TRUE),
              `1-Little`= sum(`1-Little`/`Number of Responses (Count)`, na.rm = TRUE) * 100,
              `2` = sum(`2` / `Number of Responses (Count)`, na.rm = TRUE) * 100,
              `Do not know/ Not applicable` = sum(`Do not know/ Not applicable` / `Number of Responses (Count)`, na.rm = TRUE)* 100,
              `3` = sum(`3` / `Number of Responses (Count)`, na.rm = TRUE) * 100,
              `4-Much` = sum(`4-Much` / `Number of Responses (Count)`, na.rm = TRUE) * 100,
              `no_response` = sum(no_response / `Number of Responses (Count)`, na.rm = TRUE) * 100
    ) %>% 
    mutate_if(is.numeric, round, digits = 2) %>% 
    arrange(desc(`Number of Responses (Count)`))
}

目前,我有这个,但相信我需要某种基于名称(tbl)的 ifelse / case_when() 循环,但我在编程方面真的很新,不知道从哪里开始。summarise 函数中的 col 名称需要与它们正在汇总的输入表的名称相同。

    prop_function <- function(tbl){
  tbl %>% 
  summarise(`Number of Responses` = sum(3:length(.), na.rm = TRUE))
}

我不需要一个完整的解决方案,任何小的想法和贡献都是有帮助的。如果这是一个重复的问题类型,感谢您在正确的方向上进行指导。

之后我还将这些输入到 purr::map() + ggplot() 中,所以如果解决方案有点 tidyverse 友好,我将不胜感激。

干杯。

标签: rdplyrpurrr

解决方案


这是一个继续使用 dplyr/tidyverse 并从Prop_Function_Group1(tbl1). 但是,此功能应该能够应用于您描述的其他表格。

library(tidyverse)

prop_function <- function(tbl){
tbl_counts <- tbl %>% 
  summarise_if(is.double, ~sum(.x))

tbl_counts %>% 
  mutate_all(~100 * .x / sum(tbl_counts)) %>%
  mutate(`Number of Responses (Count)` = sum(tbl_counts)) %>% 
  mutate_all(round, digits = 2) %>% 
  select(length(.), everything()) # move last col to first
}

list(tbl1, tbl2) %>% 
  map(prop_function)
#> [[1]]
#> # A tibble: 1 x 7
#>   `Number of Resp~ `1-Little`   `2`   `3` `4-Much` `Do not know/ N~
#>              <dbl>      <dbl> <dbl> <dbl>    <dbl>            <dbl>
#> 1               68       11.8  5.88  23.5     47.1             5.88
#> # ... with 1 more variable: no_response <dbl>
#> 
#> [[2]]
#> # A tibble: 1 x 8
#>   `Number of Resp~ `1-Little`   `2`   `3` `4-Much` `5-MuchMuch`
#>              <dbl>      <dbl> <dbl> <dbl>    <dbl>        <dbl>
#> 1               76       10.5  5.26  21.0     42.1         5.26
#> # ... with 2 more variables: `Do not know/ Not applicable` <dbl>,
#> #   no_response <dbl>

reprex 包(v0.2.1)于 2019 年 1 月 10 日创建


推荐阅读