Search code examples
rgoogle-trends

How can I get gtrendsR to combines multiple 90-day periods of Trends data?


I am currently trying to get multiple 90-day periods into a single data frame using a for loop in R. I need the 90-day periods because that is the only way that Google Trends gives daily trends data. Here is what I have so far... However, when this runs I get a data frame with 0 observations and 0 variables. The "dates_ranges" are just the 90-day periods I am trying to collect Trends data on.

dates_ranges=c("2010-01-01 2010-04-01","2010-04-01 2010-06-30","2010-06-30 2010-10-01","2010-10-01 2011-01-01", "2011-01-01 2011-04-01","2011-04-01 2011-06-30","2011-06-30 2011-10-01","2011-10-01 2012-01-01")

gtrendsBPA <- data.frame()

for (i in 1:length(dates_ranges)) { rbind(gtrendsBPA, (gtrends("BPA", geo="US", time=dates_ranges[i])$interest_over_time))}


Solution

  • You almost have it, you just need to reassign gtrendsBPA to itself in the for loop, as seen below:

    gtrendsBPA = data.frame()
    for (i in 1:(length(dates_ranges))) { 
      gtrendsBPA = rbind(gtrendsBPA, (gtrends("BPA", geo="US", time=dates_ranges[i])$interest_over_time))
    }
    

    This should give you the results you need.