ifelse using multiple values

I have updated this post to include reprex.

I am trying to categorize these part numbers as "roll" otherwise "sheet"
This code runs without error, but it finds no "TRUE" conditions so every value is called "sheet" yet I have validated these values do exist in the data. What am I doing wrong?

structure(list(ccmasterid = c("D18385      ", "D18448      ", 
"D18448      ", "D18419X     ", "D18385      ", "Z18466      ", 
"Z18466      ", "CV2236      ", "CV2236      ", "CV2236      ", 
"Z18471      ", "D18376      ", "D18384      ", "Z18451      ", 
"D18384      ", "D18214AV    ", "D18214AV    ", "S18275      ", 
"D18214AV    ", "D18214AV    "), ccjobpart = c("01   ", "06   ", 
"05   ", "01   ", "01   ", "02   ", "01   ", "03   ", "02   ", 
"01   ", "01   ", "02   ", "02   ", "01   ", "01   ", "01   ", 
"02   ", "01   ", "03   ", "04   "), jcmasterid = c("01525", 
"01525", "01525", "01525", "01525", "01525", "01525", "01525", 
"01525", "01525", "01525", "01525", "01525", "01525", "01525", 
"01525", "01525", "01525", "01525", "01525"), icmasterid = c("STY040A05101220                                   ", 
"KOM003W06001200                                   ", "KOM003W06001200                                   ", 
"SFB0380W125404896                                 ", "STY040A05101220                                   ", 
"STY040AW125405199                                 ", "INF50BLK18004296                                  ", 
"CV3MIJ40C20W5412                                  ", "CV3MIJ40C20W5412                                  ", 
"CV3MIJ40C20W5412                                  ", "SFB050W125406096                                  ", 
"SFB12019W1254042100                               ", "ECB200B28504855                                   ", 
"ULB050W06001200                                   ", "ECB200B28504855                                   ", 
"SFB12019W1254050100                               ", "SFB12019W1254050100                               ", 
"ULB100BK0048096                                   ", "ULB019W1254051099                                 ", 
"ULB019W1254051099                                 ")), row.names = c(NA, 
20L), class = "data.frame")

JobSS$MatType <- ifelse(JobSS$icmasterid %in% c("MTADHCLREMO05412", "OVL004CLEMBOSS05412", "OVL004CLEMBOSS06112", "OVL005POLYPRM05112", "VBN013W2S012612",
              "CV3M40CMW006012", "CV3MIJ40C20W5412", "PAB020SIGNWRIT04812", "PP008TPQ2BO05712", "PSV008RAMW05412", "VBN0131SDULT05412", "VBN0131SDULT06312", "VBN013W2SBO05412", 'VMG020W04000120', "VMR010WFRP06012", "VPS004CLAD05412", "VPS004WBOPM5412",
              "VPS004WBOPM6012", "VPS004WHFG05412", "VPS004WPRM05412", "VPS004WRMV05412", "VPS3MIJ180C5412", "VPS3MIJ180C6012", "VWP008NOLAR05412", "VWP020DRMAT5412", "VWP020DRMAT6012", "VWP020DRSUE5412", "VWP020DSSUE6012"), "roll", "sheet")
#> Error in JobSS$icmasterid %in% c("MTADHCLREMO05412", "OVL004CLEMBOSS05412", : object 'JobSS' not found

Created on 2019-09-06 by the reprex package (v0.3.0)

Apparently there is nothing wrong with your code so maybe the problem is with the structure of your dataset, could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

It looks to me like you're not looping over all rows in JobSS, but assigning the first instance of the test of JobSS$icmasterid to all rows of JobSS$MatType.

This might do the trick:

library(tidyverse)

newJobSS <- JobSS %>%
      mutate(MatType = ifelse(icmasterid %in% c("MTADHCLREMO05412", "OVL004CLEMBOSS05412", "OVL004CLEMBOSS06112", "OVL005POLYPRM05112", "VBN013W2S012612", "CV3M40CMW006012", "CV3MIJ40C20W5412", "PAB020SIGNWRIT04812", "PP008TPQ2BO05712", "PSV008RAMW05412", "VBN0131SDULT05412", "VBN0131SDULT06312", "VBN013W2SBO05412", 'VMG020W04000120', "VMR010WFRP06012", "VPS004CLAD05412", "VPS004WBOPM5412", "VPS004WBOPM6012", "VPS004WHFG05412", "VPS004WPRM05412", "VPS004WRMV05412", "VPS3MIJ180C5412", "VPS3MIJ180C6012", "VWP008NOLAR05412", "VWP020DRMAT5412", "VWP020DRMAT6012", "VWP020DRSUE5412", "VWP020DSSUE6012"), "roll", "sheet"))

ifelse() is vectorized so I don't think that is the case, see this example

df <- data.frame(stringsAsFactors = FALSE,
                 letters = c("a", "b", "c", "d", "e"))
df$test <- ifelse(df$letters %in% c("a", "c", "e"), "roll", "sheet")
df$test
#> [1] "roll"  "sheet" "roll"  "sheet" "roll"

Created on 2019-09-05 by the reprex package (v0.3.0.9000)

1 Like

Thanks, I stand corrected.

Not having seen the actual contents of JobSS$icmasterid, it's hard to give a solid answer. Is it possible that not all of the letters in JobSS$icmasterid are upper case? Perhaps try

ifelse(toupper(JobSS$icmasterid) %in% ......
1 Like

I am trying to categorize these part numbers as "roll" otherwise "sheet"
This code runs without error, but it finds no "TRUE" conditions so every value is called "sheet" yet I have validated these values do exist in the data. What am I doing wrong?


data.frame(stringsAsFactors=FALSE,
+    ccmasterid = c("D18385      ", "D18448      ", "D18448      ",
+                   "D18419X     ", "D18385      ", "Z18466      ",
+                   "Z18466      ", "CV2236      ", "CV2236      ", "CV2236      ",
+                   "Z18471      ", "D18376      ", "D18384      ", "Z18451      ",
+                   "D18384      ", "D18214AV    ", "D18214AV    ", "S18275      ",
+                   "D18214AV    ", "D18214AV    "),
+     ccjobpart = c("01   ", "06   ", "05   ", "01   ", "01   ", "02   ",
+                   "01   ", "03   ", "02   ", "01   ", "01   ", "02   ",
+                   "02   ", "01   ", "01   ", "01   ", "02   ", "01   ", "03   ",
+                   "04   "),
+    icmasterid = c("STY040A05101220                                   ",
+                   "KOM003W06001200                                   ",
+                   "KOM003W06001200                                   ",
+                   "SFB0380W125404896                                 ",
+                   "STY040A05101220                                   ",
+                   "STY040AW125405199                                 ",
+                   "INF50BLK18004296                                  ",
+                   "CV3MIJ40C20W5412                                  ", "CV3MIJ40C20W5412                                  ",
+                   "CV3MIJ40C20W5412                                  ",
+                   "SFB050W125406096                                  ",
+                   "SFB12019W1254042100                               ",
+                   "ECB200B28504855                                   ",
+                   "ULB050W06001200                                   ",
+                   "ECB200B28504855                                   ", "SFB12019W1254050100                               ",
+                   "SFB12019W1254050100                               ",
+                   "ULB100BK0048096                                   ",
+                   "ULB019W1254051099                                 ",
+                   "ULB019W1254051099                                 ")
+ )

JobSS$MatType <- ifelse(JobSS$icmasterid %in% c("MTADHCLREMO05412", "OVL004CLEMBOSS05412", "OVL004CLEMBOSS06112", "OVL005POLYPRM05112", "VBN013W2S012612",
              "CV3M40CMW006012", "CV3MIJ40C20W5412", "PAB020SIGNWRIT04812", "PP008TPQ2BO05712", "PSV008RAMW05412", "VBN0131SDULT05412", "VBN0131SDULT06312", "VBN013W2SBO05412", 'VMG020W04000120', "VMR010WFRP06012", "VPS004CLAD05412", "VPS004WBOPM5412",
              "VPS004WBOPM6012", "VPS004WHFG05412", "VPS004WPRM05412", "VPS004WRMV05412", "VPS3MIJ180C5412", "VPS3MIJ180C6012", "VWP008NOLAR05412", "VWP020DRMAT5412", "VWP020DRMAT6012", "VWP020DRSUE5412", "VWP020DSSUE6012"), "roll", "sheet")
#> Error in JobSS$icmasterid %in% c("MTADHCLREMO05412", "OVL004CLEMBOSS05412", : object 'JobSS' not found

Created on 2019-09-06 by the reprex package (v0.3.0)

Your columns have a lot of empty spaces at the end of the strings, if you remove them your code works

library(stringr)

JobSS <- data.frame(stringsAsFactors=FALSE,
                 ccmasterid = c("D18385      ", "D18448      ", "D18448      ",
                                "D18419X     ", "D18385      ", "Z18466      ",
                                "Z18466      ", "CV2236      ", "CV2236      ", "CV2236      ",
                                "Z18471      ", "D18376      ", "D18384      ", "Z18451      ",
                                "D18384      ", "D18214AV    ", "D18214AV    ", "S18275      ",
                                "D18214AV    ", "D18214AV    "),
                 ccjobpart = c("01   ", "06   ", "05   ", "01   ", "01   ", "02   ",
                               "01   ", "03   ", "02   ", "01   ", "01   ", "02   ",
                               "02   ", "01   ", "01   ", "01   ", "02   ", "01   ", "03   ",
                               "04   "),
                 jcmasterid = c("01525", "01525", "01525", "01525", "01525", "01525",
                                "01525", "01525", "01525", "01525", "01525", "01525",
                                "01525", "01525", "01525", "01525", "01525", "01525", "01525",
                                "01525"),
                 icmasterid = c("STY040A05101220                                   ",
                                "KOM003W06001200                                   ",
                                "KOM003W06001200                                   ",
                                "SFB0380W125404896                                 ",
                                "STY040A05101220                                   ",
                                "STY040AW125405199                                 ",
                                "INF50BLK18004296                                  ",
                                "CV3MIJ40C20W5412                                  ", "CV3MIJ40C20W5412                                  ",
                                "CV3MIJ40C20W5412                                  ",
                                "SFB050W125406096                                  ",
                                "SFB12019W1254042100                               ",
                                "ECB200B28504855                                   ",
                                "ULB050W06001200                                   ",
                                "ECB200B28504855                                   ", "SFB12019W1254050100                               ",
                                "SFB12019W1254050100                               ",
                                "ULB100BK0048096                                   ",
                                "ULB019W1254051099                                 ",
                                "ULB019W1254051099                                 ")
)

JobSS$icmasterid <- str_trim(JobSS$icmasterid) # Remove empty spaces

JobSS$MatType <- ifelse(JobSS$icmasterid %in% c("MTADHCLREMO05412", "OVL004CLEMBOSS05412", "OVL004CLEMBOSS06112", "OVL005POLYPRM05112", "VBN013W2S012612",
                                                "CV3M40CMW006012", "CV3MIJ40C20W5412", "PAB020SIGNWRIT04812", "PP008TPQ2BO05712", "PSV008RAMW05412", "VBN0131SDULT05412", "VBN0131SDULT06312", "VBN013W2SBO05412", 'VMG020W04000120', "VMR010WFRP06012", "VPS004CLAD05412", "VPS004WBOPM5412",
                                                "VPS004WBOPM6012", "VPS004WHFG05412", "VPS004WPRM05412", "VPS004WRMV05412", "VPS3MIJ180C5412", "VPS3MIJ180C6012", "VWP008NOLAR05412", "VWP020DRMAT5412", "VWP020DRMAT6012", "VWP020DRSUE5412", "VWP020DSSUE6012"), "roll", "sheet")
JobSS$MatType
#>  [1] "sheet" "sheet" "sheet" "sheet" "sheet" "sheet" "sheet" "roll" 
#>  [9] "roll"  "roll"  "sheet" "sheet" "sheet" "sheet" "sheet" "sheet"
#> [17] "sheet" "sheet" "sheet" "sheet"
2 Likes

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