汇总多个回答问题 [英] Tabulating multiple response questions
问题描述
想象一下,我有一个问题,其中有四个选项,而受访者可以选择零或这四个选项的任意组合.这些变量分别命名为A
,B
,C
和D
,并且响应存储在data.frame中,如下所示.
Imagine that I have a question for which there are four options, and a respondent can select zero or any combination of the four. The variables are named A
, B
, C
, and D
and the responses are stored in a data.frame as below.
set.seed(1)
dat = data.frame(A = sample(c(0, 1), 20, replace=TRUE),
B = sample(c(0, 1), 20, replace=TRUE),
C = sample(c(0, 1), 20, replace=TRUE),
D = sample(c(0, 1), 20, replace=TRUE))
我可以通过以下方法将响应的组合制成表格(例如,单独使用A
或A
+ B
或C
+ D
等等,有多少响应):
I can tabulate the combination of responses (for example, how many responded with A
alone, or A
+B
, or C
+D
, and so on) by doing the following:
data.frame(table(dat))
# A B C D Freq
# 1 0 0 0 0 2
# 2 1 0 0 0 2
# 3 0 1 0 0 0
# 4 1 1 0 0 1
# 5 0 0 1 0 1
# 6 1 0 1 0 3
# 7 0 1 1 0 0
# 8 1 1 1 0 2
# 9 0 0 0 1 0
# 10 1 0 0 1 2
# 11 0 1 0 1 1
# 12 1 1 0 1 1
# 13 0 0 1 1 2
# 14 1 0 1 1 0
# 15 0 1 1 1 3
# 16 1 1 1 1 0
我现在想创建一个新列,以显示此输出表示的字母组合.例如,第4行代表A
+ B
响应的计数,第14行代表A
+ C
+ D
响应的计数.
I would like to now create a new column that shows the letter combination that is being represented by this output. For example, row 4 represents the count of A
+B
responses, and row 14 represents the count of A
+C
+D
responses.
我认为apply
函数之一在这里很有用,但是我不确定如何进行.
I think that one of the apply
functions would be useful here, but I'm not sure how to proceed.
推荐答案
dat.t <- data.frame(table(dat))
dat.t$combn <- apply(dat.t[,1:4] == 1, 1, function(x) paste(names(dat)[x], collapse=' + '))
> dat.t
A B C D Freq combn
1 0 0 0 0 2
2 1 0 0 0 2 A
3 0 1 0 0 0 B
4 1 1 0 0 1 A + B
5 0 0 1 0 1 C
6 1 0 1 0 3 A + C
7 0 1 1 0 0 B + C
8 1 1 1 0 2 A + B + C
9 0 0 0 1 0 D
10 1 0 0 1 2 A + D
11 0 1 0 1 1 B + D
12 1 1 0 1 1 A + B + D
13 0 0 1 1 2 C + D
14 1 0 1 1 0 A + C + D
15 0 1 1 1 3 B + C + D
16 1 1 1 1 0 A + B + C + D
>
这篇关于汇总多个回答问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!