pjbudden pjbudden - 1 year ago 156
R Question

chaining ifelse with mutate not working

I have a dataframe which looks something like:

placeID <- c(001, 002, 003, 004, 005)
faultDscr <- c("6940 broken", "6122 not working", "6122 broken", "6940 not working", "printer not working")
partID <- c("PTR-HP-6940", NA, "PTR-HP-6122", NA, NA)
myData <- data.frame(placeID, faultDscr, partID)

I want to create a new variable to identify if a site is using a 6122 inkjet as part of my dplyr pipeline. The new variable should contain either a Y or an N. I have used the code below:

inkjetSites <- myData %>%
mutate(inkjetSite = ifelse(partID == "PTR-HP-6122", "Y",
ifelse(is.na(partID) && grep("6122", faultDscr), "Y", "N")))

I'd hoped to get an output like:

inkjetSite <- c("N", "Y", "Y", "N", "N")
inkjetSitesCor <- data.frame(placeID, faultDscr, partID, inkjetSite)

However I instead got an output like:

inkjetSite <- c("N", NA, "Y", NA, NA)
inkjetSitesInCor <- data.frame(placeID, faultDscr, partID, inkjetSite)

Any help would be greatly appreciated.

Answer Source

We can try with %in% and remove the == as the NA will remain as NA there (unless we create an & !is.na(partID)), change the && to & and also change from grep to grepl (for logical vector output)

myData %>% 
     mutate(inkjetSite = ifelse(partID %in% "PTR-HP-6122", "Y", 
              ifelse(is.na(partID) & grepl("6122", faultDscr), "Y", "N")))
#  placeID           faultDscr      partID inkjetSite
#1       1         6940 broken PTR-HP-6940          N
#2       2    6122 not working        <NA>          Y
#3       3         6122 broken PTR-HP-6122          Y
#4       4    6940 not working        <NA>          N
#5       5 printer not working        <NA>          N

This can be also done without an ifelse

myData %>%
   mutate(inkjetSite =  c("N", "Y")[partID %in% "PTR-HP-6122" + 
              (is.na(partID) & grepl("6122", faultDscr)) + 1])
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download