'Conditional recoding gives no error, but no changes either

I am trying to recode some variables in an experiment with two lists ("list1" and "list2").

The id was supposed to match between each experimental condition, but unfortunately they don't. The only solution I have found to fix this was with the recode_if() function:

olives <- olives %>%
    mutate(id = recode_if(id, id == 1 & list == "list1", "1" = "9"))

olives

Although R gives me no error message, I am not seeing any changes.

My intention with this is something like the following:

  • If item is in list1 and id is 1, change id = 1 to id = 9

  • If item is in list1 and id is 2, change id = 2 to id = 10

etc.

Where could be the error?

 dput(head(olives))

structure(list(subject = c(NA, 128, 128, 128, 128, 128), X2 = c(NA, 
NA, NA, NA, NA, NA), birth_year = c(NA, 1997, 1997, 1997, 1997, 
1997), X4 = c(NA, NA, NA, NA, NA, NA), birth_month = c(NA, 8, 
8, 8, 8, 8), X6 = c(NA, NA, NA, NA, NA, NA), native_language = c(NA, 
"Greek", "Greek", "Greek", "Greek", "Greek"), X8 = c(NA, NA, 
NA, NA, NA, NA), Multilingual = c(NA, "Yes", "Yes", "Yes", "Yes", 
"Yes"), X10 = c(NA, NA, NA, NA, NA, NA), Dyslexic = c(NA, "No", 
"No", "No", "No", "No"), X12 = c(NA, NA, NA, NA, NA, NA), Sex = c(NA, 
"Female", "Female", "Female", "Female", "Female"), X14 = c(NA, 
NA, NA, NA, NA, NA), HandPreference = c(NA, "Right", "Right", 
"Right", "Right", "Right"), X16 = c(NA, NA, NA, NA, NA, NA), 
    rt = c(NA, NA, NA, 5602, NA, NA), stimulus = c(NA, NA, NA, 
    "Η μεταφορική εταιρία είναι στη Λάρισα", 
    NA, NA), trial_index = c(NA, 15, 17, 18, 20, 22), time_elapsed = c(NA, 
    139855, 146511, 152621, 159248, 167898), list = c(NA, "list1", 
    "list1", "list1", "list1", "list1"), button_pressed = c(NA, 
    NA, NA, 0, NA, NA), rt1 = c(NA, 313, 216, NA, 246, 191), 
    rt2 = c(NA, 545, 139, NA, 196, 327), rt3 = c(NA, 337, 416, 
    NA, 212, 320), rt4 = c(NA, 139, 373, NA, 314, 426), rt5 = c(NA, 
    396, 326, NA, 345, 427), rt6 = c(NA, 333, 631, NA, 540, 481
    ), id = c(NA, 14, 1, 1, 7, 11), item_type = c(NA, "ZZM", 
    "ZYM", "QZYM", "ZYM", "ZZMM"), expected_answer = c(NA, NA, 
    NA, "Λάθος", NA, NA), answer = c(NA, NA, NA, "Λάθος", 
    NA, NA), correct = c(NA, NA, NA, TRUE, NA, NA), integer_correct = c(NA, 
    NA, NA, 1, NA, NA)), row.names = c(NA, 6L), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))


Solution 1:[1]

Try %in%: The issue might be exact match with ==.

library(dplyr)

olives %>% 
  mutate(id = case_when(item %in% list1 & id == 1 ~9, 
                        item %in% list2 & id == 2 ~10,
                        TRUE ~ id))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TarJae