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(): 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
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.