Testing whether a value matches a list of values

10,488

as suggested by @dickoa you can use the %in% keyword , you can do the following

condition <- df$col1 %in% c("Bob" , "Tom" , "Dick" , "Harry")

df$col2[condition] <- "Boy's name"
  • How it works

for the first line condition <- df$col1 %in% c("Bob" , "Tom" , "Dick" , "Harry") it will check every value in df$col1 if it matches any of the following names Bob" , "Tom" , "Dick" , "Harry and if it finds a match it will return True and if it didn't it will return false . so the result now will be a vector of values of True and false.

when you pass the resulting vector as index to df$col2 it will give only the values in df$col2 that matches values of True and ignore false values , so now you can edit these values

Share:
10,488
user4896331
Author by

user4896331

Updated on June 04, 2022

Comments

  • user4896331
    user4896331 almost 2 years

    This seems really obvious, but I can't find the answer anywhere.

    I have a two columns (Col1, Col2). I want to check each row of Col1 for a match with a few keywords and if I find a match, write something into Col2. I'm doing it like this:

    df$Col2[df$Col1=="Bob"]<-"Boy's name"
    

    The problem I have is that I have lots of different names to check, so I'm ending up with a huge statement along the lines of:

    df$Col2[df$Col1=="Bob" | df$Col1=="Tom" | df$Col1=="Dick" | df$Col1=="Harry"]<-"Boy's name"
    

    I'd prefer to do this:

    df$Col2[df$Col1=="Bob|Tom|Dick|Harry"]<-"Boy's name"
    

    The above statement runs without an error, but it doesn't work either: it doesn't write anything to COl2. I'm guessing that it's not correctly evaluating the "Bob|Tom|Dick|Harry" bit. What am I'm doing wrong?