Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8185137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:44:26+00:00 2026-06-07T01:44:26+00:00

Imagine that I have a question for which there are four options, and a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:44:27+00:00Added an answer on June 7, 2026 at 1:44 am
    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
    > 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked the question this way because I can imagine that there's a potentially
I would like to ask you: Imagine that you have a site and there
Imagine that HTML page is a game surface (see picture). User can have n
Imagine I have two (three, four, whatever) tasks that have to run in parallel.
Imagine that I have a form in a flash application with two fields, input1
Imagine that I have a nice Deck class, in the best OO fashion. It
Imagine that i have the following code: <a:repeat value=#{bean.getList()} var=x > <li class=la> <span>
imagine that I have a Form with 9 controls (TabbedStuffControl) in a 3x3 tile,
Let's imagine that I have to make a table with the following structure with
Let's imagine that I have list of files at host1 find /path/to -name *.jpg

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.