3

I'm trying to automate creating variables indicating whether students' answer (variables beginning with l,m, f or g) to the questions (eg. variables starting in "test_") are correct or not. ie. This done by checking whether, for example, test_l1 == l1.

I cannot figure out how to do this other than using the index, but it's very tedious and creates a lot of codes.

Below is a toy dataset that mimics the structure of the actual dataset which has 4 different kinds of tests with 12 exercises each (test_l1 ~ test_l12, test_m1 ~ test_m12, test_f1~,test_g1~) and corresponding student responses (l1~l12, m1~m12, f1~, g1~). I would like to create 48 variables that are namely correct_l1 ~ correct_l12, correct_m1~, correct_f1~ etc.)

df <- data.frame(test_l1 = c(1,0,0), 
                 test_l2=c(1,1,1), 
                 test_m1 = c(0,1,0), 
                 test_m2=c(0,1,1), 
                 l1=c(0,1,0), 
                 l2=c(1,1,1), 
                 m1=c(1,1,1), 
                 m2=c(0,0,1))

Many thanks in advance!!!

2 Answers 2

3

Here is a tidyverse solution you can use:

library(dplyr)

df %>%
  mutate(across(starts_with("test_"), ~ .x == get(sub("test_", "", cur_column())), 
                .names = '{gsub("test_", "answer_", .col)}'))

  test_l1 test_l2 test_m1 test_m2 l1 l2 m1 m2 answer_l1 answer_l2 answer_m1 answer_m2
1       1       1       0       0  0  1  1  0     FALSE      TRUE     FALSE      TRUE
2       0       1       1       1  1  1  1  0     FALSE      TRUE      TRUE     FALSE
3       0       1       0       1  0  1  1  1      TRUE      TRUE     FALSE      TRUE
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, that worked!! (Upvoted but I'm a stack novice and don't have the reputation pts to upvote yet :( )
Nice use of cur_column()
My pleasure dear Arun, this question was an opportunity I tweaked .names argument with a string manipulation function and again thanks to one of Anil's questions.
1

Get all the 'test' columns in test_cols, remove the string 'test_' from test_cols to get the corresponding columns to compare.

Directly compare the two dataframes and create new columns.

test_cols <- grep('test', names(df), value = TRUE)
ans_cols <- sub('test_', '', test_cols)
df[paste0('correct_', ans_cols)] <- df[test_cols] == df[ans_cols]

df
#  test_l1 test_l2 test_m1 test_m2 l1 l2 m1 m2 correct_l1 correct_l2 correct_m1 correct_m2
#1       1       1       0       0  0  1  1  0      FALSE       TRUE      FALSE       TRUE
#2       0       1       1       1  1  1  1  0      FALSE       TRUE       TRUE      FALSE
#3       0       1       0       1  0  1  1  1       TRUE       TRUE      FALSE       TRUE

where TRUE means the answer is correct and FALSE means answer is wrong.

1 Comment

Thank you so much, that worked!! (Upvoted but I'm a stack novice and don't have the reputation pts to upvote yet :( )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.