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?
Assuming that your question data.frame is
DQand your answersDTYou need to make sure that your
q_idcolumn in your answers has all the levels availablethen you can use
reshape2anddcastwithdrop = FALSEto cast as you wish