【问题标题】:R loop function for selecting an element in multiple objects用于在多个对象中选择元素的 R 循环函数
【发布时间】:2020-04-21 03:00:30
【问题描述】:

我正在使用 Google Trends R 包来执行多个关键字查询,如下所示:

trends1 <- gtrends(keyword="compare", gprop=channel,geo="AU", time=time, category=249)
trends2 <- gtrends(keyword="switch", gprop=channel,geo="AU", time=time, category=249)
trends3 <- gtrends(keyword="change", gprop=channel,geo="AU", time=time, category=249)

我只对兴趣随着时间的推移结果感兴趣,所以我将它们单独列出:

time_trend1 <- trends1$interest_over_time
time_trend2 <- trends2$interest_over_time
time_trend3 <- trends3$interest_over_time

但我有 60 个(还有更多要添加)。我想写一个重复循环(我想):

#select only interest over time
x <- 0
repeat {
time_trend(x+1) <- trends(x+1)$interest_over_time
if (x == 61){break}
}

但我得到错误:趋势错误(x + 1):找不到函数“趋势”

我错过了什么?

【问题讨论】:

    标签: r function loops


    【解决方案1】:

    您可以使用lapply 遍历关键字列表并提取请求的元素,如下所示:

    library(gtrendsR)
    time <- "today+5-y"
    channel <- "web"
    keywords <- list("compare", "switch", "change")
    trends <- setNames(lapply(keywords, function(x) gtrends(keyword=x, 
      gprop=channel, geo="AU", time=time, category=249)), keywords)
    lapply(trends, `[[`, "interest_over_time")
    #> $compare
    #>           date hits geo      time keyword gprop category
    #> 1   2015-04-26   25  AU today+5-y compare   web      249
    #> 2   2015-05-03   26  AU today+5-y compare   web      249
    #> 3   2015-05-10   41  AU today+5-y compare   web      249
    #> 4   2015-05-17   29  AU today+5-y compare   web      249
    #> 5   2015-05-24   32  AU today+5-y compare   web      249
    # ...
    #> 260 2020-04-12    9  AU today+5-y compare   web      249
    #> 
    #> $switch
    #>           date hits geo      time keyword gprop category
    #> 1   2015-04-26    0  AU today+5-y  switch   web      249
    #> 2   2015-05-03    0  AU today+5-y  switch   web      249
    #> 3   2015-05-10    0  AU today+5-y  switch   web      249
    #> 4   2015-05-17    0  AU today+5-y  switch   web      249
    #> 5   2015-05-24    0  AU today+5-y  switch   web      249
    # ...
    #> 260 2020-04-12    0  AU today+5-y  switch   web      249
    #> 
    #> $change
    #>           date hits geo      time keyword gprop category
    #> 1   2015-04-26   45  AU today+5-y  change   web      249
    #> 2   2015-05-03   68  AU today+5-y  change   web      249
    #> 3   2015-05-10   23  AU today+5-y  change   web      249
    #> 4   2015-05-17   52  AU today+5-y  change   web      249
    #> 5   2015-05-24   76  AU today+5-y  change   web      249
    # ...
    #> 260 2020-04-12   38  AU today+5-y  change   web      249
    

    reprex package (v0.3.0) 于 2020 年 4 月 20 日创建

    编辑: 一旦将单个列表元素组合到 data.table、tibble 或 data.frame 中,进一步操作数据可能是最容易的。此处显示的是如何删除不需要的列的示例。要按关键字进行子集化,可以这样做,例如res[keyword=="compare"]

    library(gtrendsR)
    library(data.table)
    time <- "today+5-y"
    channel <- "web"
    keywords <- list("compare", "switch", "change")
    trends <- setNames(lapply(keywords, function(x) gtrends(keyword=x, 
      gprop=channel, geo="AU", time=time, category=249)), keywords)
    res <- rbindlist(lapply(trends, `[[`, "interest_over_time"))
    res[,-c("geo","category","time")]
    #>            date hits keyword gprop
    #>   1: 2015-04-26   25 compare   web
    #>   2: 2015-05-03   26 compare   web
    #>   3: 2015-05-10   41 compare   web
    #>   4: 2015-05-17   29 compare   web
    #>   5: 2015-05-24   32 compare   web
    #>  ---                              
    #> 776: 2020-03-15   51  change   web
    #> 777: 2020-03-22   27  change   web
    #> 778: 2020-03-29   20  change   web
    #> 779: 2020-04-05    0  change   web
    #> 780: 2020-04-12   35  change   web
    

    reprex package (v0.3.0) 于 2020 年 4 月 21 日创建

    【讨论】:

      【解决方案2】:

      您可以使用ls + mget 获取列表中的数据,使用lapply 遍历每个列表并获取每个列表的"interest_over_time" 元素。

      total_list <- lapply(mget(ls(pattern = 'trends\\d+')), `[[`, "interest_over_time")
      

      total_list 会给你数据框列表。最好将数据保存在列表中,因为它更易于管理并且不会因为大量对象而使环境混乱。但是,如果您想要为每个单独的数据,我们可以使用list2env

      list2env(total_list, .GlobalEnv)
      

      要删除某些列,我们可以这样做:

      total_list <- lapply(mget(ls(pattern = 'trends\\d+')), function(x) {
               data <- x$interest_over_time
               data[setdiff(names(data), c("geo","category","time"))]
      })
      

      【讨论】:

      • 我们可以在这里使用 tidyeval 吗?
      • 我不确定 tidyeval 在这里会如何使用。
      • 这很好,但是我将如何从 dfs 列表中添加/删除数据列?即删除“geo”、“category”、“time”等...
      • @DrPaul 我们可以使用setdiff,查看更新后的答案。
      猜你喜欢
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多