Closed diegocgaona closed 7 years ago
Hi,
I created a question in Stackoverflow with a bounty, if anyone can help: https://stackoverflow.com/questions/46940436/getting-a-column-with-a-list-and-merging-with-the-others-in-r-with-fbrads
Thanks!
Extracting the nested lists is indeed out of the scope of the fbRads
package, but you could try something like:
## list of action types to extract
actiontypes <- c('link_click', 'comment', 'like', 'post')
## extract actions from the data returned by the Insights API
lactions <- unlist(lapply(l, function(x) x$actions), recursive = FALSE)
## extract fields from the actions
library(data.table)
lactions <- rbindlist(lapply(lactions, function(actions) {
setnames(as.data.table(
do.call(cbind,
lapply(actiontypes,
function(action) {
if (is.null(actions)) return(0)
value <- subset(actions, action_type == action, value)
if (nrow(value) == 0) return(0) else return(value[[1]])
}))),
actiontypes)
}))
I'm closing this ticket, but looking forward to your feedback on how this works.
Thanks @daroczig
It works, but, without the campaign_name. My real problem is just that, my action rows data are not in the correct campaigns rows with my code.
With your code, they came without the campaign column. I think your data is right and ordered. You know how I can include the campaign column in your code?
This is iterating over every single campaign/ad set, and returning data in the right order. Basically you can cbind
this to the rest of l
.
Hi @daroczig, yes! I tried it and worked fine! Many thanks!
I'll just pop in here randomly with another thumbs up. This is much better than my kludgy solution. I learned something this morning! Thanks @daroczig
Hi,
Sorry, this is not a issue of the package, but I tried to make this work (and find a answer) and I can't.
I'm using
fb_insights
like this: (I use more metrics in my real problem)fb_campaigns <- rbindlist(lapply(l, function(l) cbind(Campaign = l$campaign_name, rbindlist(l$actions))))
The result is the data frame with all the data I need (Campaign, action_type, value), but... the columns with the "action_types" and their numbers came out of order. The action data don't seem to be from the campaigns in the rows.
How can I merge the action types with the campaigns?
After I the data in the correct rows, I will use reshape to make the action_types columns with the values.
Many thanks, Diego.