Error in mutate function

Hi,

I have this code in R:
LAB<-LAB %>% mutate(CP= ifelse(grepl('VIM|NDM|KPC|IMP|blaOXA|OXA-48|carbapenemase', LAB$LocalOrganismDescription, ignore.case = TRUE), 1, 0))
for creating a new column CP with the values extracted from "LocalOrganismDescription" variable.
This worked perfectly, but when I added values extraction from another variable such as:
LAB<-LAB %>% mutate(CP= ifelse(grepl('VIM|NDM|KPC|IMP|blaOXA|OXA-48|carbapenemase', LAB$LocalOrganismDescription, ignore.case = TRUE)|grepl('VIM|NDM|KPC|IMP|blaOXA|OXA-48|carbapenemase', LAB$LocalTestDescription, ignore.case = TRUE), 1, 0))
I had this error:

Error in mutate():
:information_source: In argument: CP = ifelse(...).
Caused by error:
! CP must be size 6300 or 1, not 0.

I'm not sure what is the reason for this error. Any help is really appreciated.
Thank you

This works for me.

LAB <- data.frame(LocalOrganismDescription = c("A","VIM","B"),
                   LocalTestDescription = c("NDM","C","D"))
library(dplyr)
LAB<-LAB %>% mutate(CP= ifelse(grepl('VIM|NDM|KPC|IMP|blaOXA|OXA-48|carbapenemase', LAB$LocalOrganismDescription, ignore.case = TRUE)|
                                  grepl('VIM|NDM|KPC|IMP|blaOXA|OXA-48|carbapenemase', LAB$LocalTestDescription, ignore.case = TRUE), 1, 0))
LAB
  LocalOrganismDescription LocalTestDescription CP
1                        A                  NDM  1
2                      VIM                    C  1
3                        B                    D  0

Are you sure both of your column names are spelled correctly?
I suggest you drop the use of LAB$ inside of the mutate() function. Since you pass LAB to mutate(), you do not need to specify the data frame.

LAB<-LAB %>% mutate(CP= ifelse(grepl('VIM|NDM|KPC|IMP|blaOXA|OXA-48|carbapenemase', LocalOrganismDescription, ignore.case = TRUE)|
                                 grepl('VIM|NDM|KPC|IMP|blaOXA|OXA-48|carbapenemase', LocalTestDescription, ignore.case = TRUE), 1, 0))

It worked. Thank you

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.