R - create column based on words in another column

Lost Source

Is there a way to create a new column in an R dataframe based on words in another column?

Such as I have a "color" column with "Blue 1", "Blue 2", up to "Blue 6", and then I have 6 other colors with their own set of numbers after. Is there a way to create a new column that would just say "Blue", "Green", "Yellow", etc without writing out each color out itself?

color2 <- ifelse(color=="Blue 1", "Blue",
    ifelse(color=="Blue 2", "Blue",
    ifelse(color=="Blue 3", "Blue",
    ifelse(color=="Blue 4", "Blue",
    ifelse(color=="Blue 5", "Blue",
    ifelse(color=="Blue 6", "Blue",
                    NA  ))))

Obviously, I would continue with the other colors, but is there a more streamlined way to do this?



answered 1 week ago akrun #1

We can remove the digits followed by the space by creating a pattern in the sub and replace it with blank ("")

sub("\\s+\\d+", "", color)

Or with str_extract, only extract one or more letters from the start (^) of the string

str_extract(color, "^[A-Za-z]+")

comments powered by Disqus