Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gtrendsR geo MSA/Area Code

Tags:

r

geo

gtrendsr

I am gathering Google Trends data using the R Package gtrendsR. I am trying to pull data for each metropolitan statistical area (MAS) but area code would also be good. So far I have only managed to get the state-level data. Here is the code for that.

example <- gtrends("car", geo="US-FL")$interest_over_time 

I have tried the following for the MSA:

example2 <- gtrends("car", geo="US-FL-Jacksonville FL")$interest_over_time 

and for the area codes:

example3 <- gtrends("car", geo="US-FL-904")$interest_over_time 

I get errors saying that the package cannot retrieve valid codes. In data("countries") associated with the package, the codes are only for state-level - e.g. US-FL for Florida.

I would be interested in knowing how I can retrieve more granular data with this package, along the lines described in example2 and example3 above.

like image 207
Rebecca Avatar asked May 13 '18 17:05

Rebecca


1 Answers

To retrieve data for "Jacksonville, FL", you should use geo = "US-FL-561":

example2 <- gtrends("car", geo = "US-FL-561")$interest_over_time

To find the geo code for cities, you can use this code (you can replace "US-FL" by any country-states code you want):

data("countries")
codes <- unique(countries$sub_code[substr(countries$sub_code, 1,5) == "US-FL"])
codes

#[1] US-FL     US-FL-571 US-FL-592 US-FL-561 US-FL-528 US-FL-534 US-FL-656 US-FL-539 US-FL-548 US-FL-530

countries[countries$sub_code %in% codes[2:length(codes)],]

#       country_code  sub_code                                name
#122665           US US-FL-571                Ft. Myers-Naples, FL
#122666           US US-FL-592                     Gainesville, FL
#122667           US US-FL-561                    Jacksonville, FL
#122668           US US-FL-528            Miami-Ft. Lauderdale, FL
#122670           US US-FL-534 Orlando-Daytona Beach-Melbourne, FL
#122671           US US-FL-656                     Panama City, FL
#122672           US US-FL-539  Tampa-St Petersburg (Sarasota), FL
#122673           US US-FL-548      West Palm Beach-Ft. Pierce, FL
#122680           US US-FL-530     Tallahassee, FL-Thomasville, GA

Function

If easier, you can also write the code as a function:

city_code <- function(geo){
  codes <- unique(countries$sub_code[substr(countries$sub_code, 1,5) == geo])
  if(length(codes) > 1){
    countries[countries$sub_code %in% codes[2:length(codes)], 2:3]
  } else{
    message('No city code for this geo')
  }
}

Examples

city_code("US-AL")

#        sub_code                                        name
#122636 US-AL-630                              Birmingham, AL
#122637 US-AL-606                                  Dothan, AL
#122638 US-AL-691           Huntsville-Decatur (Florence), AL
#122639 US-AL-698                      Montgomery (Selma), AL
#122669 US-AL-686 Mobile, AL-Pensacola (Ft. Walton Beach), FL

city_code("US-CA")

#        sub_code                                          name
#122649 US-CA-800                               Bakersfield, CA
#122650 US-CA-868                             Chico-Redding, CA
#122651 US-CA-802                                    Eureka, CA
#122652 US-CA-866                            Fresno-Visalia, CA
#122653 US-CA-803                               Los Angeles, CA
#122654 US-CA-828                          Monterey-Salinas, CA
#122655 US-CA-804                              Palm Springs, CA
#122656 US-CA-862               Sacramento-Stockton-Modesto, CA
#122657 US-CA-825                                 San Diego, CA
#122658 US-CA-807            San Francisco-Oakland-San Jose, CA
#122659 US-CA-855 Santa Barbara-Santa Maria-San Luis Obispo, CA
like image 152
DJack Avatar answered Oct 08 '22 11:10

DJack