Scoring

First, build a data frame for the (made up) correct answers

correct.answers = data.frame(
      item = 1:4,
      correct = c(3,1,4,1)
   )
item correct
1 3
2 1
3 4
4 1

i.e., the correct answer for item 1 is 3, for item 2 it's 1…

Now invent some actual answers for 4 pretend participants

actual.answers = data.frame(
      subject = c(1,1,1,1, 2,2,2,2, 3,3,3,3, 4,4,4,4),
      item    = rep(c(1,2,3,4),4),
      answer =  c(3,1,4,1, 3,2,4,1, 3,2,0,1, 1,2,3,4)
   )
subject item answer
1 1 3
1 2 1
1 3 4
1 4 1
2 1 3
2 2 2
2 3 4
2 4 1
3 1 3
3 2 2
3 3 0
3 4 1
4 1 1
4 2 2
4 3 3
4 4 4

Sub 1 has done very well. Sub 4 hasn't done so well.

Merge the two tables, and add an extra columns which is TRUE iff the correct answer was given

scored = merge(actual.answers,correct.answers)
scored$is.correct = scored$correct == scored$answer

Now build a table to get a sumscore for each person

scored.table = table(scored$subject, scored$is.correct)
scored.table

and make into a dataframe again

scored.df = data.frame(
   subject=dimnames(scored.table)[[1]],
   score = scored.table[,2]
)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License