Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shuffle exercise list while fixing one or more elements

Tags:

r

r-exams

Let's say I've got 20 questions in a folder and I want to create an exam of 10 questions out of these 20, so that each student can have a different exam, using exams2pdf or exams2nops.

I'd also like a specific set of one or more exercises to always be included in the exam.

This would work:

library(exams)
fixed_question <- "questions/a.Rmd" 
remaining_questions <- setdiff(paste0("questions/", dir(path = "questions"), fixed_question)) 
exam <- list(fixed_question, remaining_questions)
nsamp <- c(1,9)
exams2pdf(exam, n = 5, nsamp = nsamp)

but fixed_question will always be the first question in the exam. Is there a way to shuffle the order of appearance of all 10 questions, including the fixed one(s)?

I also tried

exam <- list(c(fixed_question, sample(remaining_questions, 9)))
nsamp <- 10
exams2pdf(exam, n = 5, nsamp = nsamp)

Or even

exam <- list(sample(c(fixed_question, sample(remaining_questions, 9))))

but now I'm missing the other 10 questions.

like image 788
JayBee Avatar asked Sep 03 '25 04:09

JayBee


1 Answers

Advanced shuffling patterns (as well as specific fixed patterns) can best be achieved by setting by an n-by-k matrix of file names, where

  • n is the number of exams
  • k is the numer of exercises per exam
  • and the matrix contains the desired permutations of the questions

This can then be passed to exams2pdf() or exams2nops() and the arguments n= and nsamp= can then be dropped. Based on the example in the question, you can first select the vectors of exercise names:

library("exams")
fixed_question <- "a.Rmd" 
remaining_questions <- setdiff(dir(path = "questions"), fixed_question)

Note that I have dropped the questions/ path from the names above. This is best added as the edir= argument below.

Then you can:

  • sample 5 exams with 9 exercises from the remaining_questions
  • add the fixed_question in the first position
  • shuffle all 5 exams again
exam <- replicate(5, sample(remaining_questions, 9))
exam <- rbind(fixed_question, exam)
exam <- t(apply(exam, 2, sample))

And then finally you call exams2pdf() without the n or nsamp arguments (and here with the edir argument as explained above).

exams2pdf(exam, edir = "questions/")
like image 68
Achim Zeileis Avatar answered Sep 05 '25 01:09

Achim Zeileis