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 7955037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:36:43+00:00 2026-06-04T03:36:43+00:00

I have a data set in R as follows ID Variable1 Variable2 Choice 1

  • 0

I have a data set in R as follows

ID  Variable1  Variable2 Choice
1   1          2         1
1   2          1         0
2   2          1         1
2   2          1         1

I need to get the output table for it as under

Id Variable1-1 Variable1-2 Variable2-1 Variable2-2
1  1           0           0           1
2  0           2           2           0

Note that only those rows are counted where the choice is 1 (choice is a binary variable, however other variables have any integer values). The aim is to have as many columns for a variable as its levels.

Is there a way I can do this in R?

  • 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-04T03:36:45+00:00Added an answer on June 4, 2026 at 3:36 am

    You could use melt and dcast from the reshape2 package:

    mydf<-read.table(text="ID  Variable1  Variable2 Choice
    1   1          2         1
    1   2          1         0
    2   2          1         1
    2   2          1         1",header=TRUE)
    
    library(reshape2)
    

    First melt the data.frame, selecting only those rows where Choice == 1 and removing the Choice column

    mydfM <- melt(mydf[mydf$Choice %in% 1, -match("Choice", names(mydf))], id = "ID")
    
    # EDIT above: As @TylerRinker points out, using which could be avoided.
    # I've replaced it with %in%
    
    #   ID  variable value
    # 1  1 Variable1     1
    # 2  2 Variable1     2
    # 3  2 Variable1     2
    # 4  1 Variable2     2
    # 5  2 Variable2     1
    # 6  2 Variable2     1
    

    Then cast the melted data.frame, using length as the aggregation function

    (mydfC <- dcast(mydfM, ID ~ variable + value, fun.aggregate = length))
    
    #   ID Variable1_1 Variable1_2 Variable2_1 Variable2_2
    # 1  1           1           0           0           1
    # 2  2           0           2           2           0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data set that is around 700 rows with eight columns of
I have a data set that that I would like to call in a
I have a data set of item difficulties that correspond to items on a
I have the following data set that I am trying to plot with ggplot2,
I have a large data set that I'm working with in excel. About 1000+
I have an Indian company data set and need to extract the City and
I am trying to output a set of data, and I have, due to
I have a hibernate mapping as follows: <hibernate-mapping> <class name=kochman.elie.data.types.InvoiceTVO table=INVOICE> <id name=id column=ID>
I have the following problem. I have a data set that has the beginning
I have a data set with over 50,000 geocoded points (lat-long). Each point has

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.