Getting combo column and merging with others in R - with fbRads

I use fb_insightsfbRads from the package as follows: (I use more indicators in my real problem)

fb_campaigns <- rbindlist(lapply(l, function(l) cbind(Campaign = l$campaign_name, rbindlist(l$actions))))

Oh, and I get some warnings (I know that I am doing something wrong, but I cannot solve it):

Warning messages: 1: In data.table::data.table(...) : Item 1 is of size 11 but maximum size is 104 (recycled leaving remainder of 5 items)

The result is a data frame with all the data I need (Campaign, action_type, value), but ... the columns with "action_types" and their numbers are out of order. The action data does not seem to be from campaigns in rows.

How can I combine action types with campaigns?

After entering the data in the correct rows, I will use reshape to make action_types columns with values.

The data I get from fb Rads and I want to convert: enter image description here

, , ( , , )

enter image description here

+4
1

daroczig , , , !

## 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)
            }))

## Merging the dataframe with the original data and the dataframe with the actions
    fb_campaigns <- cbind(l[,c(1,4:11)],lactions))
0

Source: https://habr.com/ru/post/1688182/


All Articles