mercredi 24 février 2021

Nesting ifelse in R to mutate across multiple columns

I am wrangling data in R from an experiment with, for simplicity, 2 questions and 3 conditions. Each participant is assigned to 1 of the 3 conditions, every participant answers both questions.

The data is currently organized so that each column contains the answers for one question within one condition:

Respondent.ID <- c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6", "ID7", "ID8", "ID9")
Q1.CondA <- c("Correct", "Incorrect", "I don't know", "", "", "", "", "", "")
Q1.CondB <- c("", "", "", "Incorrect", "Correct", "I don't know", "", "", "")
Q1.CondC <- c("", "", "", "", "", "", "I don't know", "Correct", "Incorrect")
Q2.CondA <- c("Incorrect", "Correct", "I don't know", "", "", "", "", "", "")
Q2.CondB <- c("", "", "", "I don't know", "Correct", "Incorrect", "", "", "")
Q2.CondC <- c("", "", "", "", "", "", "Correct", "Incorrect", "I don't know")

current<- data.frame(Respondent.ID, Q1.CondA, Q1.CondB, Q1.CondC, Q2.CondA, Q2.CondB, Q2.CondC)

I want to reorganize the data so that one column shows the assigned condition, one column show the answers for question 1 for all conditions, and 1 column shows the answers for question 2 for all conditions:

Respondent.ID <- c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6", "ID7", "ID8", "ID9")
Condition <- c("A", "A", "A", "B", "B", "B", "C", "C", "C")
Q1 <- c("Correct", "Incorrect", "I don't know", "Incorrect", "Correct", "I don't know", "I don't know", 
"Correct", "Incorrect")
Q2 <- c("Incorrect", "Correct", "I don't know", "I don't know", "Correct", "Incorrect", "Correct", "Incorrect", "I don't know")
desired<- data.frame(Respondent.ID, Condition, Q1, Q2)

I can do this in Excel by nesting If-statements (IF cell non-blank, THEN duplicate cell, ELSE IF next cell non-blank, THEN duplicate next cell, ELSE IF...). But I would rather do everything in R if possible.

The drag to copy function in Excel means that I can enter this cell-level nested If-statement once, and then it is automatically changed for the other cells. My actual dataset has 40 questions, 8 conditions, and 800 participants, so doing this manually isn't an option.

In R, I have tried combining mutate() with nested ifelse() statements:

dplyr::mutate(current, Condition = ifelse(current$Q1.CondA != ' ', 'A', 
       ifelse(current$Q1.CondB !=' ', 'B',
              ifelse(current$Q1.CondC !=' ', 'C', ' '))))

But it's just looking up the first element in the column, finding a nonblank, and then returning a new 'Condition' column that is filled with 'A'.

Is there a way get this to work in R to get from my current data frame to my desired data frame?

Aucun commentaire:

Enregistrer un commentaire