For loop replacement has length zero

Hi everyone,

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
}

Thanks so much in advance for helping me out!

Best, Wouter

This sounds like a situation where case_when() might help. But it would be great if you could show us a sample of your data.

Thanks a lot for your reply. I will look into case_when(). Please see a sample of my data below. I hope this helps!

> hf_keywords_new
# A tibble: 89 x 5
   Keyword                      `Search Volume` `Keyword Difficulty`   CPC sv_trig
   <chr>                                  <dbl>                <dbl> <dbl>   <dbl>
 1 hubspot templates free                    90                 68.9  0         NA
 2 hubspot agency                            50                 67.8 16.7       NA
 3 hubspot email templates free              70                 58.8  0         NA
 4 hubspot digital marketing                 50                 84.7  8.68      NA
 5 benefits of hubspot cms                   50                 51.6  0         NA
 6 hubspot case study template               70                 79.8  0         NA
 7 hubspot marketing agency                  90                 63.6  0        510
 8 hubspot audit template                    50                 53.1  0         NA
 9 hubspot marketing                        170                 90.6  3.87      NA
10 hubspot landing page                      70                 69.5  9.22      NA
> hf_ad_groups
# A tibble: 3 x 11
  word1   word2         n count_trigram count_kw sv_trig sv_total avg_cpc avg_dif ratio_seo ratio_sea
  <chr>   <chr>     <int>         <dbl>    <dbl>   <dbl>    <dbl>   <dbl>   <dbl>     <dbl>     <dbl>
1 hubspot sales         2             3        5     190      370    1.10    74.3    0.0148     67.4 
2 hubspot marketing     5             7       10     510      860    5.33    78.2    0.0681     14.7 
3 hubspot demo          2             2        3     140      230    7.74    69.2    0.112       8.94

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.

library(tidyverse)
library(fuzzyjoin)

hf_keywords_new <- tibble(Keyword = c("hubspot templates free", "hubspot agency", 
                                      "hubspot marketing agency", "hubspot marketing"))

hf_ad_groups <- tibble(word1 = rep("hubspot", 3),
                       word2 = c("sales", "marketing", "demo"),
                       sv_trig = c(190, 510, 140))

hf_ad_groups <- unite(hf_ad_groups, word, c(word1, word2), sep = " ")

fuzzy_left_join(hf_keywords_new, hf_ad_groups, by = c(Keyword = "word"), match_fun = str_detect)
#> # A tibble: 4 x 3
#>   Keyword                  word              sv_trig
#>   <chr>                    <chr>               <dbl>
#> 1 hubspot templates free   <NA>                   NA
#> 2 hubspot agency           <NA>                   NA
#> 3 hubspot marketing agency hubspot marketing     510
#> 4 hubspot marketing        hubspot marketing     510

Created on 2020-05-12 by the reprex package (v0.3.0)

1 Like

This is amazing, thank you so much!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.