1

In R I have a table of people's responses to a multiple choice questionnaire e.g. table1:

subject    question1 question2 question3
person1    1         2         1  
person2    2         1         3
person3    4         3         1
person4    2         4         2

and I have tables I want to use to combine their answers to the questions to produce a new variable e.g. table2:

       Q2=1      Q2=2     Q2=3     Q2=4
  Q1=1  1         1        2        2
  Q1=2  2         2        2        3
  Q1=3  3         3        4        4
  Q1=4  4         4        4        5

Where the row# is their answer to Question 1 and the column# is their answer to Question 2. For example, if they answered "3" to Question 1 and "2" to Question 2, I need the corresponding value in row 3/column 2 of the second table i.e. "3".

When I use the code:

table1$combinedq1q2 = (table2[table1$question1, table1$question2])

I'm hoping to end up with:

subject    question1 question2 question3 combinedq1q2
person1    1         2         1         1
person2    2         1         3         2
person3    4         3         1         4
person4    2         4         2         3

but instead, R is generating a bajillion columns named combinedq1q2.1, combinedq1q2.2, combinedq1q2.3, etc... I don't understand why this is happening or how to fix it. ): I'd appreciate any help!

I did look at this thread and if I can't find any other solution I'll just copy the code from there without really understanding it. But since what I'm looking up values by is the same as the row and column numbers, I was hoping there would be a simpler solution.

Thanks!

EDIT: Thank you for the responses! I added row and column names to my Table 2 to clarify. I'm trying to use the answers to Question 1 and Question 2 to look up/access the value of a third variable in Table 2 (and I already have Table 2; I do not need to generate it). Also, combinedq1q2 just happened to always match question1 in my previous example, so I added another person/row to show that this won't always be the case.

I'll try what was suggested so far, although I'm confused about people saying I'll get combinedq1q2=5 for person3.

3
  • 2
    Your outputs don't make sense. For example, person3 answered 4 to question1 and 3 to question2, so according to you, table2[4, 3] should equal 4, but you have it as 5. Also, what will you do if multiple people had the same answers to question 1 and question2? Or if no persion had that (question1, question2) combination? (in your example table2, you have (1, 3) having the value 3, but no-one answered question 1 with a 1 and question2 with a 3. In addition, your "Im hoping to end up with" combinedq1q2 seems to just be the same as question1 and does not match your table2. Commented Oct 7, 2016 at 2:52
  • 1
    table1$combinedq1q2 <- mapply(function(q1, q2){table2[q1, q2]}, table1$question1, table1$question2) Commented Oct 7, 2016 at 2:55
  • Oh, I see - is it that you have table2 (whose definition doesn't really match your explanation), but that you want to access values from table2? Or that you want to generate table2? Commented Oct 7, 2016 at 2:57

1 Answer 1

1

I think your question is "How do I look up the (question1, question2)th (row, col) of table2 and save the result for each person", where table2 is given. (As opposed to "how do I generate table2").

if so, you can use matrix indexing into your table. Ie make a 2-column matrix where each row is the (row, column) coordinates, and use that to index table2.

df$question1question2 = table2[cbind(df$question1, df$question2)]

However this output doesn't match yours. For example, person3 has (question1, question2) = (4,3) and that element of table2 is 5, but in your desired output you have 4 (in fact, it looks like you just want question1question2 to be the same as question1.

You will have to clarify your desired output.

If your questions is around how to generate a table2, you'll have to answer my comment about how exactly this table is constructed.

Sign up to request clarification or add additional context in comments.

2 Comments

Both your solution and alistaire's worked--thank you for your help! Could anyone enlighten me as to why simply table1$combinedq1q2 = (table2[table1$question1, table1$question2]) didn't work as intended?
I believe that if you supply both i and j to [ you will get a matrix with all the rows of table1$question1 and all the columns in table1$question2. E.g. if you put in table2[c(1,2,4, 2), c(2, 1, 3, 4)] you will get rows 1, 2, 4, 2(again) and columns 2, 1, 3, 4 of table2. (AFAIK this is not documented in ?'[' and you're not really meant to use this to index. The recommended way as written in the help file if you want one number per row of coordinates is to provide the coordinates as a 2-column matrix)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.