【问题标题】:gtrendsR Google Trends in RgtrendsR 谷歌 R 趋势
【发布时间】:2021-08-09 17:07:15
【问题描述】:

关于 Google 趋势包 gtrendsR 的问题。

我的代码如下:

    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 给了我两个错误:(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 值。

我的问题是我应该如何编写代码以获取多个国家? 每次运行代码时我应该如何编码日期以获得最近的日期?

谢谢。

【问题讨论】:

    标签: r gtrendsr


    【解决方案1】:

    我在您的方法中发现了两个问题:

    1. keyword 参数有问题,geo 没有问题。我猜不出来。也许它的长度。
    2. 时间跨度的格式错误:"2018-01-01 CurrentDate" 在gtrends的帮助下:

    "Y-m-d Y-m-d" 两个日期之间的时间跨度(例如:"2010-01-01 2010-04-03")。

    问题 #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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-04
      相关资源
      最近更新 更多