如何使用同一调用在多个配置文件上使用 RGA 包在 R 中调用 google 分析 api
How to call the google analytics api in R with the RGA package on multiple profiles using the same call
使用 R 中的 RGA 包,通常你会调用 API 类似的东西:
start.date <- "2015-01-01"
profile.id <- 8131437
end.date <- format(Sys.Date(), format = "%Y-%m-%d")
hey <- get_ga(profile.id, start.date, end.date,
dimensions = "ga:minute, ga:day, ga:month",
metrics = "ga:sessions")
其中 get_ga
是 RGA 包中记录的用于获取数据的函数。
但是,我有一个数据 table 包含我想要为其获取数据的所有配置文件,我正在尝试使用 for 循环高效地执行此操作,如下所示:
for (i in 1:nrows(UK_profiles)) {
allData <- get_ga([i], start.date, end.date,
dimensions = "ga:minute, ga:day, ga:month",
metrics = "ga:sessions")
row.names(allData) <- paste([i], 1:nrows(allData), sep ="")
}
您可以看到我也在尝试粘贴回个人资料 ID,以便稍后可以按此进行细分。
我的错误是:
错误:意外的“[”在:
所有数据 <- get_ga(["
错误:“ row.names(allData) <- paste(["
中出现意外的“[”
任何帮助都会很棒。
尝试这样的事情:
library(RGA)
authorize()
ids <- list_profiles()$id
res <- lapply(ids, function(id) {
ans <- get_ga(id, start.date = "2015-01-01", end.date = Sys.Date(),
dimensions = "ga:minute, ga:day, ga:month",
metrics = "ga:sessions")
ans$id <- id
return(ans)
})
res <- do.call(rbind, res)
为了加速,您可以将 lapply
替换为 parallel
包 (mclapply
) 中的替代项。
使用 R 中的 RGA 包,通常你会调用 API 类似的东西:
start.date <- "2015-01-01"
profile.id <- 8131437
end.date <- format(Sys.Date(), format = "%Y-%m-%d")
hey <- get_ga(profile.id, start.date, end.date,
dimensions = "ga:minute, ga:day, ga:month",
metrics = "ga:sessions")
其中 get_ga
是 RGA 包中记录的用于获取数据的函数。
但是,我有一个数据 table 包含我想要为其获取数据的所有配置文件,我正在尝试使用 for 循环高效地执行此操作,如下所示:
for (i in 1:nrows(UK_profiles)) {
allData <- get_ga([i], start.date, end.date,
dimensions = "ga:minute, ga:day, ga:month",
metrics = "ga:sessions")
row.names(allData) <- paste([i], 1:nrows(allData), sep ="")
}
您可以看到我也在尝试粘贴回个人资料 ID,以便稍后可以按此进行细分。
我的错误是: 错误:意外的“[”在:
所有数据 <- get_ga([" 错误:“ row.names(allData) <- paste(["
中出现意外的“[”任何帮助都会很棒。
尝试这样的事情:
library(RGA)
authorize()
ids <- list_profiles()$id
res <- lapply(ids, function(id) {
ans <- get_ga(id, start.date = "2015-01-01", end.date = Sys.Date(),
dimensions = "ga:minute, ga:day, ga:month",
metrics = "ga:sessions")
ans$id <- id
return(ans)
})
res <- do.call(rbind, res)
为了加速,您可以将 lapply
替换为 parallel
包 (mclapply
) 中的替代项。