I have a dataframe:
df <- data.frame(name=c("john", "david", "callum", "joanna", "allison", "slocum", "lisa"), id=1:7) df name id 1 john 1 2 david 2 3 callum 3 4 joanna 4 5 allison 5 6 slocum 6 7 lisa 7
I have a vector containing a regular expression that I want to find in the df $ name variable:
vec <- c("lis", "^jo", "um$")
The output I want to get is as follows:
name id group 1 john 1 2 2 david 2 NA 3 callum 3 3 4 joanna 4 2 5 allison 5 1 6 slocum 6 3 7 lisa 7 1
I could do this by doing the following:
df$group <- ifelse(grepl("lis", df$name), 1, ifelse(grepl("^jo", df$name), 2, ifelse(grepl("um$", df$name), 3, NA)
However, I want to do this directly from 'vec'. I am generating various values ββin vec reactively in a brilliant application. Can i assign groups by index in vec?
Further, if something like below happens, the group should be the first. for example, "Callum" has a value of TRUE for "all" and "um $", but should get group 1 here.
vec <- c("all", "^jo", "um$")