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.