简体   繁体   English

gtrendsR 谷歌趋势在 R

[英]gtrendsR Google Trends in R

A question about Google Trends package gtrendsR.关于 Google 趋势 package gtrendsR 的问题。

I have written my code as follows:我编写了如下代码:

    install.packages('gtrendsR')
    library(gtrendsR)

# Define the key words

    keywords = c("x", "y", "d", "e", "f")

# Set the geographic area: DE = Germany; DK = Denmark

    country = c('DE','DK')

# Set the time window

    CurrentDate <- Sys.Date()
    time=("2018-01-01 CurrentDate")

# Set channels

    channel = 'web'
    trends = gtrends(keywords, gprop = channel, geo = country, time = time)

R gives me two errors: (1) Error in gtrends(keywords, gprop = channel, geo = country, time = time): (length(keyword)%%length(geo) == 0) || (length(geo)%%length(keyword) ==.... is not TRUE R 给了我两个错误: (1) Error in gtrends(keywords, gprop = channel, geo = country, time = time): (length(keyword)%%length(geo) == 0) || (length(geo)%%length(keyword) ==.... is not TRUE (1) Error in gtrends(keywords, gprop = channel, geo = country, time = time): (length(keyword)%%length(geo) == 0) || (length(geo)%%length(keyword) ==.... is not TRUE which appears because I try to use two locations; (2) Cannot parse the supplied time format., if I only leave 'DE' for the country. Then, it does not read the CurrentDate value. (1) Error in gtrends(keywords, gprop = channel, geo = country, time = time): (length(keyword)%%length(geo) == 0) || (length(geo)%%length(keyword) ==.... is not TRUE ,因为我尝试使用两个位置;(2) 无法解析提供的时间格式。如果我只留下 'DE'国家。然后,它不会读取 CurrentDate 值。

My question is how I should write the code to get more than one country at the time?我的问题是我应该如何编写代码以获取多个国家? And how should I code the date to get the most recent date every time I run the code?每次运行代码时,我应该如何编码日期以获得最近的日期?

Thank you.谢谢你。

I found two issues in your approach:我在您的方法中发现了两个问题:

  1. Something is wrong with the keyword argument and not with geo . keyword参数有问题,而不是geo I could not guess it.我猜不出来。 Maybe its length.也许它的长度。
  2. Time span is given in a wrong format: "2018-01-01 CurrentDate" From the help of gtrends :时间跨度以错误的格式给出: "2018-01-01 CurrentDate"来自gtrends的帮助:

"Ymd Ymd" Time span between two dates (ex.: "2010-01-01 2010-04-03"). “Ymd Ymd” 两个日期之间的时间跨度(例如:“2010-01-01 2010-04-03”)。

Issue #1 prevents the occurence of issue #2.问题#1 防止问题#2 的发生。

library(gtrendsR)

# Define the key words
# DOES NOT WORK: keywords <- c("x", "y", "d", "e", "f")
# WORKS:
keywords <- c("y", "d", "e", "z")

# Set the time window
time <- paste0("2018-01-01", Sys.Date())

# Set channels
channel <- "web"

# Set the geographic area: DE = Germany; DK = Denmark
country <- c("DE", "DK")

res <- gtrends(keywords, gprop = channel, country, time = time)

# output
head(res$interest_over_time)
        date hits keyword geo                  time gprop category
1 2018-01-07   34       y  DK 2018-01-01 2021-05-21   web        0
2 2018-01-14   36       y  DK 2018-01-01 2021-05-21   web        0
3 2018-01-21   36       y  DK 2018-01-01 2021-05-21   web        0
4 2018-01-28   34       y  DK 2018-01-01 2021-05-21   web        0
5 2018-02-04   27       y  DK 2018-01-01 2021-05-21   web        0
6 2018-02-11   33       y  DK 2018-01-01 2021-05-21   web        0

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

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