Hi there! I have a df like this:
df<-data.frame(products=c('1 kg pears','appears to be a dog','a pear','apples red','red apple','1 kg
anana','1 kg banana'))
and I have a vector of products:
vector<-c('pear','apple','banana','anana')
I need to classify each product in df, based on the words in the vector. I was thinking about something like
df$class<-NA
for(i in 1:length(vector)){
rows_product<-which(grepl(vector[[i]],df[[1]]))
df$class[rows_product]<-vector[[i]]
}
But I realized I need to look for the words to start like the words in the vector, so if I am looking to match 'pear' it does not match 'appears', or if I am looking for 'anana' does not match 'banana'.
There is any way I can do this? I think there might be a way to do it with regex but i could not find how.