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))
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
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.
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
>