I am pretty new to R and I use it mainly for marketing data analysis.
I created a separate dataframe (hf_ad_groups) with tidyverse in order to categorise certain search phrases (hf_keywords_new). Now I want to get some information about the categorised hf_ad_groups back to the hf_keywords_new dataframe.
First, I created a new column (sv_trig = NA). Now I want to fill this column with row specific values based on another column hf_keywords_new$Keyword. If I do this for hf_keywords_new$Keyword[7] this works perfectly but if I create a for loop it doesn't.
hf_keywords_new <- mutate(hf_keywords, sv_trig = NA)
test <- hf_ad_groups %>% filter(str_detect(hf_keywords_new$Keyword[7], hf_ad_groups$word1) & str_detect(hf_keywords_new$Keyword[7], hf_ad_groups$word2))
hf_keywords_new$sv_trig[7] <- test$sv_trig
for (i in 1:length(hf_keywords_new$Keyword)){
test <- hf_ad_groups %>% filter(str_detect(hf_keywords_new$Keyword[i], hf_ad_groups$word1) & str_detect(hf_keywords_new$Keyword[i], hf_ad_groups$word2))
hf_keywords_new$sv_trig[i] <- test$sv_trig
}
Thank you for the sample data. If I understand correctly, you want to join these two tables based on whether the value in Keyword appears in the reference groups in hf_ad_groups and then retrieve the corresponding columns from the second table (sv_trig in the example above).
This is a case of inexact matching so the regular join functions found in dplyr won't work. However, you can use the fuzzyjoin package instead as shown below.