I am trying to use tidyverse functions to create a function that applies a scoring key, for multiple-choice question (MCQ) data, to a tibble. I can apply the scoring key to each column, where each column is an MCQ on a test, but the output is always transposed! Any ideas about how to apply the key to a tibble and then return a tibble that isn't transposed in any funky way? Here's where I'm at so far.
key <- c("A", "B", "C", "C") # The answers to each MCQ below
df <- tibble(
I1 = sample( LETTERS[1:4], 4, replace = T),
I2 = sample( LETTERS[1:4], 4, replace = T),
I3 = sample( LETTERS[1:4], 4, replace = T),
I4 = sample( LETTERS[1:4], 4, replace = T)
)
df1 <- df %>% mutate_all( # The scores end up transposed here
function( x) {
if_else( x == key, 1, 0)
}
)
df1
Thanks, Nirgrahamuk. I think I was misinterpreting what was going on when I applied mutate_all(). I would like to apply the key columnwise, but I would like values in the first column of the df checked against the first element in the key vector, values in the second column checked against the second element in the key vector, etc. Any ideas on how to do this?