Import and append data frames through for loop with google trends

203 views Asked by At

I have a question about how to append data frame obtaining from the gtrendsR function.

My keywords are each "california" and "texas" and my geographic locations are each "US" and "FR". So, I have 4 trends (california and US, alifornia and FR, texas and US, and texas and FR).

Here is my code.

library(gtrendsR)
state <- c("california", "texas")
nation <- c("US", "FR")

for (i in state) {
  for (j in nation){
    g <- gtrends(keyword = i,geo = j,time ="2020-01-01 2020-01-05",gprop = "web")[[1]]  
    print(g)  
    }
  }

So, I have 4 data frames with for loop. But, I could not append them into one data frame. Please help me solve this problem.

Thanks!

1

There are 1 answers

1
Emily Halford On

This should work! You should end up with a dataframe called "google_data" which includes all of your output.

library(gtrendsR)
state <- c("california", "texas")
nation <- c("US", "FR")

google_data = data.frame()

for (i in state) {
  for (j in nation){
    g <- gtrends(keyword = i,geo = j,time ="2020-01-01 2020-01-05",gprop = "web")[[1]]  
    output <- data.frame(g)
    google_data <- rbind(google_data,output) 
    }
}