using map function to create a dataframe from google trends data

532 views Asked by At

relatively new to r, I have a list of words I want to run through the gtrendsr function to look at the google search hits, and then create a tibble with dates as index and relevant hits for each word as columns, I'm struggling to do this using the map functions in purr,

I started off trying to use a for loop but I've been told to try and use map in the tidyverse package instead, this is what I had so far:


library(gtrendsr)

words = c('cruise', 'plane', 'car')
for (i in words) {
  rel_word_data = gtrends(i,geo= '', time = 'today 12-m')
  iot <- data.frame()
  iot[i] <- rel_word_data$interest_over_time$hits
}

I need to have the gtrends function take one word at a time, otherwise it will give a value for hits which is a adjusted for the popularity of the other words. so basically, I need the gtrends function to run the first word in the list, obtain the hits column in the interest_over_time section and add it to a final dataframe that contains a column for each word and the date as index.

I'm a bit lost in how to do this without a for loop

2

There are 2 answers

0
Peter K On BEST ANSWER

Assuming the gtrends output is the same length for every keyword, you can do the following:

# Load packages
library(purrr)
library(gtrendsR)

# Generate a vector of keywords
words <- c('cruise', 'plane', 'car')

# Download data by iterating gtrends over the vector of keywords
# Extract the hits data and make it into a dataframe for each keyword
trends <- map(.x = words,
              ~ as.data.frame(gtrends(keyword = .x, time = 'now 1-H')$interest_over_time$hits)) %>%
    # Add the keywords as column names to the three dataframes
    map2(.x = .,
         .y = words,
         ~ set_names(.x, nm = .y)) %>%
    # Convert the list of three dataframes to a single dataframe
    map_dfc(~ data.frame(.x))

# Check data
head(trends)
#>   cruise plane car
#> 1     50    75  84
#> 2     51    74  83
#> 3    100    67  81
#> 4     46    76  83
#> 5     48    77  84
#> 6     43    75  82
str(trends)
#> 'data.frame':    59 obs. of  3 variables:
#>  $ cruise: int  50 51 100 46 48 43 48 53 43 50 ...
#>  $ plane : int  75 74 67 76 77 75 73 80 70 79 ...
#>  $ car   : int  84 83 81 83 84 82 84 87 85 85 ...

Created on 2020-06-27 by the reprex package (v0.3.0)

1
Ronak Shah On

You can use map to get all the data as a list and use reduce to combine the data.

library(purrr)
library(gtrendsr)
library(dplyr)

map(words, ~gtrends(.x,geo= '', time = 'today 12-m')$interest_over_time %>%
                dplyr::select(date, !!.x := hits)) %>%
    reduce(full_join, by = 'date')


#         date cruise plane car
#1  2019-06-30     64    53  96
#2  2019-07-07     75    48  97
#3  2019-07-14     73    48 100
#4  2019-07-21     74    48 100
#5  2019-07-28     71    47 100
#6  2019-08-04     67    47  97
#7  2019-08-11     68    56  98
#.....