lundi 13 juin 2016

Extract pattern from string in R without distinguishing between upper and lower case letters

This is a toy example. I want to search within a and extract those colors that are listed in b. Even if the color does not start with an upper case letter, I want to extract it. However, the output should tell me how the color was used in a.

So the answer I would like to get is #"Red" NA "blue.

a <- "She has Red hair and blue eyes"
b <- c("Red", "Yellow", "Blue")
str_extract(a, b)#"Red" NA    NA

I used str_extract from 'stringr', but would be happy to use another function/package (e.g., grep).

Aucun commentaire:

Enregistrer un commentaire