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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:48:44+00:00 2026-06-18T14:48:44+00:00

I have some data frames which hold the results of a survey. The first

  • 0

I have some data frames which hold the results of a survey. The first frame lists the question ids (q_id) for each question in the survey:

  q_id
1   q1
2   q2
3   q3

The second data frame holds responses (res) for each subject (s_id) for every question that subject responded to. A subject can skip questions:

  s_id q_id res
1    1   q1   a
2    2   q1   b
3    1   q2   b

What I want to generate is a table which shows the responses to each question, where the columns are the question ids and each row represents a subject. In the above examples, the table would look like this:

  q1 q2  q3
1  a  b  NA
2  b  NA NA

What is the best way to generate such a table?

  • 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-18T14:48:45+00:00Added an answer on June 18, 2026 at 2:48 pm

    Assuming that your question data.frame is DQ and your answers DT

    You need to make sure that your q_id column in your answers has all the levels available

    DT$q_id <- factor(as.character(DT$q_id), levels = levels(DQ$q_id))
    

    then you can use reshape2 and dcast with drop = FALSE to cast as you wish

    library(reshape2)
    dcast(DT, s_id~q_id, value.var = 'res', drop = FALSE)
      s_id q1   q2   q3
    1    1  a    b <NA>
    2    2  b <NA> <NA>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have four data.frames which all have the same columns, being the first the
I'm trying to do some data manipulation in R. I have 2 data frames,
I'm having some trouble indexing data.frames in R. I'm an R beginner. I have
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame that is some 35,000 rows, by 7 columns. it
I have a data frame where some of the columns contain NA values. How
I have a data frame with an id column and some (potentially many) columns
I have a data frame with a grouping variable 'ID' and some values ('Value'):
I have a data frame in R that has come about from running some
I have a large data.frame displaying some weird properties when plotted. I'd like to

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.