有没有办法从两个或多个列表中自动提取元素?
Is there a way to automate extraction of elements from two or more lists?
我的目标是自动获取趋势数据以及从列表中提取 "interest_over_time"
。
将其视为两步问题:
根据一组关键词自动抓取数据
有条不紊地自动提取元素
我无法完成第 2 步。
关于如何完成这项工作有什么想法吗?
第 1 步 - 自动获取 google 趋势数据
library(gtrendsR)
a <- c("sony", "apple")
for (i in a) {
name <- (paste(i, sep=""))
assign(name, gtrends(keyword=i, time="now 1-H"))
print(name)
}
Step2 - 提取元素
sony[["interest_over_time"]]
我可以使用 for 或其他一些函数来自动执行此操作,而不是像上面那样手动执行此操作吗?
您可以使用 sapply
阅读。数据将存储到列表 l
.
library(gtrendsR)
l <- sapply(c("sony", "apple"), function(i) gtrends(keyword=i, time="now 1-H"))
使用 l[[1]]
和 l[[2]]
访问数据。
head(l[[1]])
# date hits keyword geo gprop category
# 1 2019-04-21 09:14:00 74 sony world web 0
# 2 2019-04-21 09:15:00 72 sony world web 0
# 3 2019-04-21 09:16:00 75 sony world web 0
# 4 2019-04-21 09:17:00 84 sony world web 0
# 5 2019-04-21 09:18:00 78 sony world web 0
# 6 2019-04-21 09:19:00 83 sony world web 0
head(l[[2]])
# location hits keyword geo gprop
# 1 Falkland Islands (Islas Malvinas) NA sony world web
# 2 São Tomé & PrÃncipe NA sony world web
# 3 Seychelles NA sony world web
# 4 St. Kitts & Nevis NA sony world web
# 5 Hong Kong 100 sony world web
# 6 Nepal 84 sony world web
我认为这里有几种方法可以满足您的需求。
library(gtrendsR)
a = c("sony","apple")
第一种方式: 使用 base
R
gtrends_interest <- function(keyword) gtrends(keyword = keyword, time = "now 1-H")[["interest_over_time"]]
trends_data <- do.call('rbind', lapply(a, gtrends_interest))
第二种方式: 使用 purrr
library(purrr)
trends_data2 <- a %>%
map_df( ~ gtrends(keyword = ., time = "now 1-H")[["interest_over_time"]])
这两种方法都会 return data.frame
与 a
中每个元素的 interest_over_time
堆叠。
我更喜欢第二种,因为一旦你掌握了它,map()
就会变得非常强大。
我的目标是自动获取趋势数据以及从列表中提取 "interest_over_time"
。
将其视为两步问题:
根据一组关键词自动抓取数据
有条不紊地自动提取元素
我无法完成第 2 步。
关于如何完成这项工作有什么想法吗?
第 1 步 - 自动获取 google 趋势数据
library(gtrendsR)
a <- c("sony", "apple")
for (i in a) {
name <- (paste(i, sep=""))
assign(name, gtrends(keyword=i, time="now 1-H"))
print(name)
}
Step2 - 提取元素
sony[["interest_over_time"]]
我可以使用 for 或其他一些函数来自动执行此操作,而不是像上面那样手动执行此操作吗?
您可以使用 sapply
阅读。数据将存储到列表 l
.
library(gtrendsR)
l <- sapply(c("sony", "apple"), function(i) gtrends(keyword=i, time="now 1-H"))
使用 l[[1]]
和 l[[2]]
访问数据。
head(l[[1]])
# date hits keyword geo gprop category
# 1 2019-04-21 09:14:00 74 sony world web 0
# 2 2019-04-21 09:15:00 72 sony world web 0
# 3 2019-04-21 09:16:00 75 sony world web 0
# 4 2019-04-21 09:17:00 84 sony world web 0
# 5 2019-04-21 09:18:00 78 sony world web 0
# 6 2019-04-21 09:19:00 83 sony world web 0
head(l[[2]])
# location hits keyword geo gprop
# 1 Falkland Islands (Islas Malvinas) NA sony world web
# 2 São Tomé & PrÃncipe NA sony world web
# 3 Seychelles NA sony world web
# 4 St. Kitts & Nevis NA sony world web
# 5 Hong Kong 100 sony world web
# 6 Nepal 84 sony world web
我认为这里有几种方法可以满足您的需求。
library(gtrendsR)
a = c("sony","apple")
第一种方式: 使用 base
R
gtrends_interest <- function(keyword) gtrends(keyword = keyword, time = "now 1-H")[["interest_over_time"]]
trends_data <- do.call('rbind', lapply(a, gtrends_interest))
第二种方式: 使用 purrr
library(purrr)
trends_data2 <- a %>%
map_df( ~ gtrends(keyword = ., time = "now 1-H")[["interest_over_time"]])
这两种方法都会 return data.frame
与 a
中每个元素的 interest_over_time
堆叠。
我更喜欢第二种,因为一旦你掌握了它,map()
就会变得非常强大。