Charles Stangor
Charles Stangor

Reputation: 304

Grading students' exams

I think there's a single line of code that grades my students' responses, but I can't quite find it. Here's an example with three questions and two students.

Thanks in advance

#the correct answers
key = t(c(1,2,3))

#the student responses
responses = t(data.frame(c(1,2,3),c(1,3,3)))
colnames(responses) =c('v1','v2','v3')
rownames(responses) = c('student1', 'student2')

#the desired graded matrix
graded = t(data.frame(c(T,T,T),c(T,F,T)))
dimnames(graded) = dimnames(responses)
graded

Upvotes: 3

Views: 238

Answers (2)

bouncyball
bouncyball

Reputation: 10761

I see that it's already been answered, oh well:

t(apply(responses, 1, FUN = function(x) x == key))

Upvotes: 1

Neal Fultz
Neal Fultz

Reputation: 9687

I'd do it this way: responses == key[col(responses)]

Upvotes: 8

Related Questions