制表多个响应问题 [英] 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的计数code>+
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屋!