16
votes

In R I want to do a like in an if statement like the example below where I'm searching for any colors in the mix$color column that contain the word red and setting a new variable in the mix dataframe to the color red.

mix$newcolor <- if(grep("Red",mix$color) "red"

And here's some sample data for the dataframe mix:

AliceBlue BlueViolet DarkRed MediumVioletRed

I'm getting this error message:

Warning message: In if (grepl("deep red", mix$color) == TRUE) "red" : the condition has length > 1 and only the first element will be used

I think that grepl should be returning a TRUE or FALSE boolean value so that should be acceptable but I'm missing something (or a lot).

Thanks for your help.

2
You need the vectorized ifelse here instead of the normal if and else. You also don't need == true when using grepl. - talat
Yes, that worked like a champ. I had considered using ifelse before but was stumped on the else part but I'll just reference mix$newcolor for the else. It will either be empty or have a converted value. Thanks. - Jazzmine
One advantage of ifelse is that you can easily nest them. Let's say you want convert your colour vector to "pure" colours: color = c("AliceBlue", "BlueViolet", "DarkRed", "MediumVioletRed", "DarkGreen"); ifelse(grepl("Red",color),"red",ifelse(grepl("Green",color),"green",ifelse(grepl("Blue",color),"blue","other"))) - xraynaud

2 Answers

22
votes

you can use grepl and an ifelse statement:

> color = c("AliceBlue", "BlueViolet", "DarkRed", "MediumVioletRed")
> ifelse(grepl("Red",color),"red","other")
[1] "other" "other" "red"  "red" 
5
votes

You don't need if or ifelse for this task. You can use sub:

color <- c("darkred", "indianred", "violetred", "deep red", 
           "Orange Red", "blue", "yellow")

sub(".*red.*", "red", color, ignore.case = TRUE)
# [1] "red"    "red"    "red"    "red"    "red"    "blue"   "yellow" 

The sub command replaces all strings including the substring "red" with "red". Furthermore, I specified ignore.case = TRUE for upper- and lowercase matches.