Using the RGA package in R, typically you would call the API with something like:
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")
Where get_ga
is the documented function in th RGA package to get the data.
However, I have a data table of all my profiles I would like to get data for, and I'm trying to do this efficiently with a for loop like so:
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 ="")
}
You can see I'm also trying to paste back in the profile ID so I can segment by this later.
My error is this: Error: unexpected '[' in:
allData <- get_ga([" Error: unexpected '[' in " row.names(allData) <- paste(["
Any help would be grand.
Try something like this:
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)
For speedup you can replace lapply
with an alternative from parallel
package (mclapply
).