I have a dataset with unique Participant_IDs that are each rated by two different Rater_IDs on many different variables (Q1, Q2, and Q3 here). I am trying to find a way to compute a variable which indicates whether the two raters' ratings are within 1 point of each other. Here's a simplified version of the data I'm working with:
library(tidyverse)
Participant_ID <- rep(1:3,2)
Rater_ID <- c(rep("A",3),rep("B",3))
Q1 <- c(5, 2, 1,3, 3, 4)
Q2 <- c(4, 2, 2,3, 5, 2)
Q3 <- c(4, 3, 3,3, 4, 5)
df <- tibble(Participant_ID, Rater_ID, Q1, Q2, Q3)
I am able to do this by spelling out each iteration of the code using below:
df <- df %>% group_by(Participant_ID) %>%
mutate(Check_Q1= ifelse((abs(Q1[1]-Q1[2]) > 1), 1, 0),
Check_Q2= ifelse((abs(Q2[1]-Q2[2]) > 1), 1, 0),
Check_Q3= ifelse((abs(Q3[1]-Q3[2]) > 1), 1, 0)) %>% ungroup()
Q1 is flagged (assigned a 1) for participant 1, Q2 is flagged for participant 2, and both Q1 and Q3 are flagged for participant 3, as the ratings have a difference > 1.
However, in my real data, there are not only 3 "Q" variables, there are many. Plus, I want this code to be used in a variety of situations where the number of Q variables will change. The user will specify the number_of_questions before running the code. I have been trying to figure out how to do this with a for loop but I cannot figure it out. This is as far as I've gotten:
number_of_questions <- 3
questions <- grep("Q", names(df), value=TRUE)
df <- df %>% group_by(Participant_ID)
for(q in questions){
for(x in 1:number_of_questions){
check_varname <- paste0("Check_Q",x)
df <- df %>%
mutate(!!check_varname := ifelse((abs(get(q)[1]-get(q)[2]) > 1), 1, 0))
}}
df <- df %>% ungroup()
I don't get any errors, but the output is not correct. It is assigning a 1 to Q1, Q2, and Q3 for Participant_ID 3. Can anyone help me understand what I'm doing wrong?