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?
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"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With