简体   繁体   English

gtrendsR geo MSA /区号

[英]gtrendsR geo MSA/Area Code

I am gathering Google Trends data using the R Package gtrendsR. 我正在使用R Package gtrendsR收集Google趋势数据。 I am trying to pull data for each metropolitan statistical area (MAS) but area code would also be good. 我正在尝试为每个大都市统计区域(MAS)提取数据,但区域代码也会很好。 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: 我为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 - eg US-FL for Florida. 在与包相关联的数据(“国家”)中,代码仅用于州级 - 例如佛罗里达州的US-FL。

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. 我有兴趣知道如何使用此包检索更细粒度的数据,沿着上面的example2和example3中描述的行。

To retrieve data for "Jacksonville, FL", you should use geo = "US-FL-561" : 要检索“Jacksonville,FL”的数据,您应该使用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): 要查找城市的地理代码,您可以使用此代码(您可以使用您想要的任何国家/地区代码替换"US-FL" ):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM