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

  • SEARCH
  • Home
  • 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 8232787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:56:42+00:00 2026-06-07T17:56:42+00:00

I do have a solution for the following problem but it’s quite ugly and

  • 0

I do have a solution for the following problem but it’s quite ugly and it can most likely be done in a much shorter way. Would be great to get your help.

My input looks like this:

C1     C2 
A      B     
B      D    
D      C 
A      D

and my output should look like this:

A B C D
1 1 0 0
0 1 0 1
0 0 1 1  
1 0 0 1

My current solution is:

index <- unique(unlist(input[,1:2]))
output <- matrix(0,nrows=dim(input),ncols=length(index))
for(i in 1:dim(input)) {
    output[i, input[i,]$C1] <- 1
    output[i, input[i,]$C2] <- 1
}

Of course 4-5 lines is actually fine – but even as an R beginner this doesn’t feel right to use a for loop. Besides that my actual data has much more than two columns, therefore this doesn’t look nice in the end. How would I do that in a smarter way?

Cheers

  • 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-07T17:56:43+00:00Added an answer on June 7, 2026 at 5:56 pm

    Here is one way:

    library(plyr)
    all.levels <- sort(levels(unlist(input)))
    adply(input, 1, function(x)table(factor(unlist(x), all.levels)))
    #   C1 C2 A B C D
    # 1  A  B 1 1 0 0
    # 2  B  D 0 1 0 1
    # 3  D  C 0 0 1 1
    # 4  A  D 1 0 0 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already have a working solution to the following problem, but I worry that
I have following problem. I worked two days on a solution but I cannot
I have trouble finding a solution for the following problem: I have a lot
I am looking for a nice code solution for the following problem. I have
I've looked around but have yet to find a great solution the the following
I am trying to solve the following problem but cannot find an elegant solution.
I can't come up with a solution to the following problem. Let's say i
I have the following problem: I have a form I need to serialize but
I'm looking for a good solution for the following problem: I have a ASP.NET
I have the following problem and I could not find a solution anywhere. I

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.