r - 获取带有列表的列并与 R 中的其他列合并 - 使用 fbRads

标签 r dataframe nested-lists facebook-marketing-api

我正在使用 fb_insights来自这样的包 fbRads :(我在我的实际问题中使用了更多的指标)
fb_campaigns <- rbindlist(lapply(l, function(l) cbind(Campaign = l$campaign_name, rbindlist(l$actions))))
哦,我收到了一些警告(我知道我做错了什么,但无法解决):
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)
结果是包含我需要的所有数据(Campaign、action_type、value)的数据框,但是......带有“action_types”的列及其数字乱序。操作数据似乎不是来自行中的广告系列。

如何将操作类型与营销事件合并?

在正确行中输入数据后,我将使用 reshape 使 action_types 列带有值。

我从 fb Rads 得到的我想转换的数据是这样的:
enter image description here

我使用我的代码获得的数据是这样的(格式可以,但不是值的顺序,它们不是广告系列的值)

enter image description here

最佳答案

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

关于r - 获取带有列表的列并与 R 中的其他列合并 - 使用 fbRads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940436/

相关文章:

r - 使用 lapply、Reduce 和 union 折叠 data.table 中的行

dataframe - 使用 gota ReadCSV 时跳过行

python - pandas groupby 转置 str 列

Python - Pandas Dataframe 获取 n 行的平均值

python - Python中列表的递归排序函数

带 stringr 的正则表达式::如何查找模式的第一个实例

r - 从 URL(Dropbox 文件夹)将多个 Parquet 文件加载到 R 中

r - R 中的 openai API : Error in chat completion - how to locate the issue?

python - 删除嵌套列表中的重复项(不删除子列表中的重复元素)

python - 删除所有空的嵌套列表