Franck
Franck

Reputation: 87

Loop in R with a function included

I'm very new at R and I would like to do a loop in order to return search volume (through an API call) for a list of keywords.

Here the code that I used :

install.packages("SEMrushR")
library(SEMrushR)

mes_keywords_to_check <- readLines("voyage.txt") # List of keywords to check
mes_keywords_to_check <- as.character(mes_keywords_to_check)

Loop

for (i in 1:length(mes_keywords_to_check)) {
  test_keyword <- as.character(mes_keywords_to_check[i])
  df_test_2 <- keyword_overview_all(test_keyword, "fr","API KEY NUMBER")  ##keyword_overview_all is the function from the Semrush package

}

By doing this, I only get the Search Volume for the first keyword in the list. My purpose if of course to get the date required for the full list of keywords.

Here is the table that I get: enter image description here

Do you have any idea how I could solve this issue?

Upvotes: 2

Views: 71

Answers (3)

Franck
Franck

Reputation: 87

Thanks for pointing to the right direction.

Here is what I did, and this is working:

final_result <- data.frame()
mes_keywords_to_check <- readLines("voyage.txt") 
mes_keywords_to_check <- as.character(mes_keywords_to_check)

for (i in 1:length(mes_keywords_to_check)) {
  test_keyword <- as.character(mes_keywords_to_check[i])
    df_test_2 <- keyword_overview_all(test_keyword, "fr","API KEY")  
  final_result <- rbind(final_result,df_test_2)
}

Upvotes: 0

Soren
Soren

Reputation: 2445

It looks like you're reading in the text file with readLines("voyage.txt") which will return a list of each line. These lines are then being passed to the for loop. The below will convert the lines to words. There are various approaches, but below uses a loop within a loop to keep using for() and in case you prefer to search line-by-line-word-by-word. It also uses a regex to split on non-alpha-numeric so that you omit words bounded by punctuation.

mes_lines <- readLines("voyage.txt") # List of keywords to check
mes_lines <- as.character(mes_lines)

search_results <- list()
for (i in 1:length(mes_lines)) {
  mes_keywords_to_check <- unlist(strsplit(mes_lines,"[^[:alnum:]]"))
  mes_keywords_to_check <- mes_keywords_to_check[nchar(mes_keywords_to_check)>0]

  if (length(mes_keywords_to_check)==0) next

  for (w in 1:length(mes_keywords_to_check))
  {
    test_keyword <- as.character(mes_keywords_to_check[w])
    print(paste0("Checking word=",test_keyword))
    df_test_2 <- keyword_overview_all(test_keyword, "fr","API KEY NUMBER")  ##keyword_overview_all is the function from the Semrush package
    search_results <- append(search_results,df_test_2)
  }

}

search_results

Upvotes: 0

vanao veneri
vanao veneri

Reputation: 1064

Well, you need to add your results to some kind of container. for example to a list. As of now, you have just one object that gets filled with data from the most recent iteration of your loop.

results = list()

for (i in 1:length(mes_keywords_to_check)) {
  test_keyword <- as.character(mes_keywords_to_check[i])
  df_test_2 <- keyword_overview_all(test_keyword, "fr","API KEY NUMBER")  ##keyword_overview_all is the function from the Semrush package
  results[[i]] <- df_test_2 
}

But, most R experts would suggest to refrain from using a loop

library("plyr")
result <- plyr::ldply(mes_keywords_to_check, function(x) keyword_overview_all(as.character(x), "fr","API KEY NUMBER"))

I did not test this, and it probably needs some tweaking, but it should point you in the right direction.

Upvotes: 1

Related Questions