Problems with If_else function of dplyr

Hello,

I am struggling with the mutate function of dplyr package. Basically, I want to compute the errors made in a specific task.

mydata2 <- mutate(mydata2, error_letter = if_else   ((letter == "A")) & ((key_DT_resp1.keys == "z" |key_DT_resp2.keys == "z")),0,1,
                                                    ((letter == "E")) & ((key_DT_resp1.keys == "x" |key_DT_resp2.keys == "x")),0,1,
                                                    ((letter == "U")) & ((key_DT_resp1.keys == "c" |key_DT_resp2.keys == "c")),0,1) 

The error message I receive is:
Problem with `mutate()` input `error_letter`. x Argument "true" fehlt (ohne Standardwert) i Input `error_letter` is `&...`.

Alternatively, I tried:

mydata2 <- mutate(mydata2, error_letter = case_when((letter == "A")) & key_DT_resp1.keys == "z" |key_DT_resp2.keys == "z"~0,
                                                   ((letter == "E")) & key_DT_resp1.keys == "x" |key_DT_resp2.keys == "x"~0,
                                                   ((letter == "U")) & key_DT_resp1.keys == "c" |key_DT_resp2.keys == "c"~0)  

Receiving:

x Input `error_letter` must be a vector, not a `formula` object.

How can I solve this issue? Any help would be appreciated.

thx in advance.

Hi,

It seems like your are using the round brackets incorrectly.
Using case_when(), this should work:

mutate(mydata2, error_letter = case_when(letter == "A" & key_DT_resp1.keys == "z" |key_DT_resp2.keys == "z"~0,
                                         letter == "E" & key_DT_resp1.keys == "x" |key_DT_resp2.keys == "x"~0,
                                         letter == "U" & key_DT_resp1.keys == "c" |key_DT_resp2.keys == "c"~0))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.