简体   繁体   中英

R - create column based on words in another column

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM