1

我经常收到来自 REDCap 调查的数据,其中允许受访者“检查”>1 对调查问题的回答。每个潜在响应都位于其自己的列中。我想总结一下检查每个响应选项(列)的频率。例如:

library(tidyverse)
set.seed(1234)
responses<-c("Checked", "Unchecked")
numobs<-10

my_example<-data.frame(id=1:10, 
                       Response_Option_A=sample(responses, numobs, replace=TRUE), 
                       Response_Option_B=sample(responses, numobs, replace=TRUE), 
                       Response_Option_C=sample(responses, numobs, replace=TRUE),
                       Response_Option_D=sample(responses, numobs, replace=TRUE),
                       stringsAsFactors = FALSE)

my_example
#>    id Response_Option_A Response_Option_B Response_Option_C Response_Option_D
#> 1   1         Unchecked         Unchecked         Unchecked           Checked
#> 2   2         Unchecked         Unchecked         Unchecked         Unchecked
#> 3   3         Unchecked         Unchecked         Unchecked           Checked
#> 4   4         Unchecked           Checked         Unchecked           Checked
#> 5   5           Checked         Unchecked         Unchecked           Checked
#> 6   6         Unchecked         Unchecked         Unchecked         Unchecked
#> 7   7           Checked         Unchecked           Checked           Checked
#> 8   8           Checked           Checked         Unchecked         Unchecked
#> 9   9           Checked         Unchecked         Unchecked         Unchecked
#> 10 10         Unchecked         Unchecked         Unchecked           Checked

我最初的想法是尝试这个,但它返回检查的响应总数,而不是每列中的数字。

my_example %>%
  select(starts_with("Response_Option_")) %>%
    summarise(checked=sum(.=="Checked"))
#>   checked
#> 1      13

reprex 包(v0.3.0)于 2020 年 8 月 10 日创建

感谢您帮助有效地总结这些响应。

4

4 回答 4

3

这是一种tidyverse显示每列而不是按行的响应总数的方法。根据您对问题的措辞,我假设这就是您要寻找的。还包括starts_with()问题标签中包含的功能。

我们可以使用pivot_longer()将响应特征从宽转换为长,然后用于group_by定义变量,获取现有表并将其转换为分组表,其中summarise() 操作用于创建新数据框,其中为分组变量的每个组合提供行.

library(tidyverse)
set.seed(1234)
responses<-c("Checked", "Unchecked")
numobs<-10

my_example<-data.frame(id=1:10, 
                       Response_Option_A=sample(responses, numobs, replace=TRUE), 
                       Response_Option_B=sample(responses, numobs, replace=TRUE), 
                       Response_Option_C=sample(responses, numobs, replace=TRUE),
                       Response_Option_D=sample(responses, numobs, replace=TRUE),
                       stringsAsFactors = FALSE)

my_example %>% 
  pivot_longer(starts_with("Response_"), names_to = "Responses", 
               values_to = "value") %>% 
  group_by(Responses, value) %>%
  summarise(total_responses = n())


#> # A tibble: 8 x 3
#> # Groups:   Responses [4]
#>   Responses         value     total_responses
#>   <chr>             <chr>               <int>
#> 1 Response_Option_A Checked                 4
#> 2 Response_Option_A Unchecked               6
#> 3 Response_Option_B Checked                 2
#> 4 Response_Option_B Unchecked               8
#> 5 Response_Option_C Checked                 1
#> 6 Response_Option_C Unchecked               9
#> 7 Response_Option_D Checked                 6
#> 8 Response_Option_D Unchecked               4

reprex 包(v0.3.0)于 2020 年 8 月 10 日创建

如果您只想要Checked响应,您可以在操作后添加以下代码行summarise()

filter(value == "Checked")

#> # A tibble: 4 x 3
#> # Groups:   Responses [4]
#>   Responses         value   total_responses
#>   <chr>             <chr>             <int>
#> 1 Response_Option_A Checked               4
#> 2 Response_Option_B Checked               2
#> 3 Response_Option_C Checked               1
#> 4 Response_Option_D Checked               6
于 2020-08-10T22:13:20.690 回答
1

检查tidyREDCap包裹。它有一堆函数来帮助处理检查来自 REDCap 的所有应用变量。该软件包位于 CRAN 上,并且位于github.io上的网站在页面顶部的文章下拉列表中有小插图。

于 2020-08-10T22:52:51.763 回答
1

您可以summarise使用across

library(dplyr)
my_example %>%
  summarise(across(starts_with("Response_Option_"), ~sum(. == 'Checked')))

#  Response_Option_A Response_Option_B Response_Option_C Response_Option_D
#1                 4                 2                 1                 6

在旧版本中,dplyr您可以使用summarise_at

my_example %>%
     summarise_at(vars(starts_with("Response_Option_")), ~sum(. == 'Checked'))
于 2020-08-11T07:50:04.610 回答
0

一个非常好的base R解决方案是:

my_example$checked <- apply(my_example[,which(grepl('Response_Option_',names(my_example)))],1,
                            function(x) length(which(x=="Checked")))

输出:

   id Response_Option_A Response_Option_B Response_Option_C Response_Option_D checked
1   1         Unchecked         Unchecked         Unchecked           Checked       1
2   2         Unchecked         Unchecked         Unchecked         Unchecked       0
3   3         Unchecked         Unchecked         Unchecked           Checked       1
4   4         Unchecked           Checked         Unchecked           Checked       2
5   5           Checked         Unchecked         Unchecked           Checked       2
6   6         Unchecked         Unchecked         Unchecked         Unchecked       0
7   7           Checked         Unchecked           Checked           Checked       3
8   8           Checked           Checked         Unchecked         Unchecked       2
9   9           Checked         Unchecked         Unchecked         Unchecked       1
10 10         Unchecked         Unchecked         Unchecked           Checked       1

对于@r2evans,这也是一种非常理想的方式:

my_example$checked <- rowSums(my_example[, grep("^Response_", colnames(my_example))] == "Checked")

这会产生相同的先前输出并且更具可读性。

于 2020-08-10T22:17:20.350 回答