daroczig / fbRads

Analyze and manage Facebook ads from R using this client library to access their Marketing APIs
GNU Affero General Public License v3.0
153 stars 57 forks source link

Doubt with fb_insights - Getting a column with a nested list and merging with the others #72

Closed diegocgaona closed 7 years ago

diegocgaona commented 7 years ago

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.

diegocgaona commented 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!

daroczig commented 7 years ago

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.

diegocgaona commented 7 years ago

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?

daroczig commented 7 years ago

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.

diegocgaona commented 7 years ago

Hi @daroczig, yes! I tried it and worked fine! Many thanks!

Cpratt3000 commented 7 years ago

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