gtrendsR geo MSA/地区代码 [英] gtrendsR geo MSA/Area Code

查看:148
本文介绍了gtrendsR geo MSA/地区代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R Package gtrendsR收集Google趋势数据.我正在尝试提取每个大都市统计区域(MAS)的数据,但是区号也很好.到目前为止,我只设法获取了状态级别的数据.这是代码.

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 

我已经针对MSA尝试了以下方法:

I have tried the following for the MSA:

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

以及区号:

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

我收到错误消息,说该程序包无法检索有效的代码.在与数据包相关联的data("countries")中,代码仅用于状态级别,例如佛罗里达的US-FL.

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.

我很想知道如何使用此程序包按照上面的example2和example3中所述的方式来检索更细粒度的数据.

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.

推荐答案

要检索佛罗里达州杰克逊维尔"的数据,应使用geo = "US-FL-561":

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

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

要查找城市的地理位置代码,可以使用以下代码(您可以将"US-FL"替换为所需的任何国家/地区代码)

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')
  }
}

示例

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

这篇关于gtrendsR geo MSA/地区代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆