I am a teacher and would like to use the package data.tablein correctly Rto automatically evaluate students' answers in the log file, i.e. add a column with a name correctif the student answers a specific question, this is the correct answer to this question, and 0 otherwise. I can do this easily if each question has only 1 answer, but I worked if the question has several possible answers (the questions and their possible correct answers are stored in another table)
The following is the MWE:
set.seed(123)
question_table <- data.table(id=c(1,1,2,2,3,4),correct_ans=sample(1:4,6,replace = T))
log <- data.table(student=sample(letters[1:3],10,replace = T),
question_id=c(1,1,1,2,2,2,3,3,4,4),
student_answer= c(2,4,1,3,2,4,4,5,2,1))
My question is the correct use of the data.tablemethod ifelsein j, especially if we are dependent on another table?
log[,correct:=ifelse(student_answer %in%
question_table[log$question_id %in% id]$correct_ans,1,0)]
As can be seen below, questions 1 and 2 have several possible correct answers.
> question_table
id correct_ans
1: 1 2
2: 1 4
3: 2 2
4: 2 4
5: 3 4
6: 4 1
, - : . student b , , . correct , , -, , .
> log
student question_id student_answer correct
1: b 1 2 1
2: c 1 4 1
3: b 1 1 1 <- ?
4: b 2 3 0
5: c 2 2 1
6: b 2 4 1
7: c 3 4 1
8: b 3 5 0
9: a 4 2 1 <- ?
10: c 4 1 1
ans log join ing question_table, , .
.
.