I am having trouble getting grepl to do an exact match when filtering a data frame using a vector. I use a reproducible example below:
my_vector<-c("Mazda", "Datsun")
my_df<-data.frame(My_vehicles=c("Mazda", "Mazda2", "Mazda3", "Datsun", "Datsun2", "Datsun3"))
If I run the below code
grepl("\\bMazda\\b",my_df$My_vehicles)
I get this as an output which is correct TRUE FALSE FALSE FALSE FALSE FALSE
I cannot however seem to get this to work within a loop. Example below
list_df<-list()
for (i in 1:length(my_vector))
{ df<-my_df%>%
filter(grepl(my_vector[i],My_vehicles))
list_df[[i]]=df
}
If I change the
filter(grepl(my_vector[i],My_vehicles))
to
filter(grepl("\\bmy_vector[i]\\b",My_vehicles))
I don't get out the right result. What I want is a list of two new data frames produced from the loop, which each should have 1 variable in which has been correctly filtered for.
Try this:
list_df<-list()
for (i in 1:length(my_vector))
{
df<-my_df%>%
filter(grepl(paste0("\\b",my_vector[i],"\\b"),My_vehicles))
list_df[[i]]=df
}