我经常收到来自 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 日创建
感谢您帮助有效地总结这些响应。