mercredi 9 novembre 2016

R: Comparing, per row in a data frame, if answers are the same as another group

I have a data set (data frame object) where Group no.1 has the "correct" answer and the other groups' answers should be compared to these. I want to make a new column with true=1/false=0 values, where I get a 1 if the answer is the same as in group 1 and a 0 if not. I'm guessing the ifelse function combined with something else can do this. Example data with expected result:

Time    Gr  Ans Correct
1      g1   A   NA
2      g1   B   NA
3      g1   C   NA
1      g2   A   1
2      g2   B   1
3      g2   B   0
1      g3   C   0
2      g3   B   1
3      g3   C   1
1      g4   A   1
2      g4   C   0
3      g4   B   0

So what I want to do is make a column, df$Correct where for each row: If Answer is identical to Answer for group1, insert 1, else insert 0.

I'm looking at "apply" or "aggregate", "identical" and "ifelse" but haven't figured out how to make R look at the column twice but in different places... Another way might be to make a new column with the correct results copied down for comparison, but there are many more variables (answer types) so I'd like to avoid that if possible.

(The question title is a bit weird, please help make it clearer if you have any suggestions!)

Thanks for any help!

Aucun commentaire:

Enregistrer un commentaire