首页 > 解决方案 > 尝试将属性设置为 NULL

问题描述

Error in data.table::rbindlist(.data, ...) : 
  attempt to set an attribute on NULL

我在以下位置收到此错误:

FBInsightsExpanded <- list.stack(list.select(FBInsightsExpanded, website_purchase_roas = website_purchase_roas$action_link_click_destination));
  print(FBInsightsExpanded)

这就是 DF 的样子。我猜我需要用 NA 替换 NULL。但到目前为止无法修复它

 website_purchase_roas  date_stop
1    0.673001 offsite_conversion.fb_pixel_purchase, 6.506892 2018-10-11
2    1.369035 offsite_conversion.fb_pixel_purchase, 0.594109 2018-10-11
3    2.084238                                           NULL 2018-10-11
4     1.31209                                           NULL 2018-10-11
5    2.337662                                           NULL 2018-10-11
6    0.996678                                           NULL 2018-10-11
7    1.936385 offsite_conversion.fb_pixel_purchase, 1.482508 2018-10-11
8    2.777778                                           NULL 2018-10-11
9           0                                           NULL 2018-10-11
10   1.994885                                           NULL 2018-10-11
11   2.402023                                           NULL 2018-10-11
12   4.635056 offsite_conversion.fb_pixel_purchase, 5.222421 2018-10-11
13          0                                           NULL 2018-10-11
14   1.990291                                           NULL 2018-10-11
15   6.557377                                           NULL 2018-10-11
16   3.703704                                           NULL 2018-10-11
17   3.038936                                           NULL 2018-10-11

代码如下所示:

library(httr)
library(fbRads)
library(rlist)
library(rhdfs)
library(Rfacebook)
library(SocialMediaLab)
library(dplyr)


app <- oauth_app('facebook', 'xxxxxxxxxx', 'xxxxxxxxxxxxxxxxxxxxxx')
fbacc <- fbad_init(accountid = id, token = "token", version = '3.0')


FBInsightsExpanded <- fb_insights(level = 'ad',
                                        time_range=sprintf("{'since':%s,'until':%s}", paste('"', maxDate, '"', sep=''), paste('"', yesterdayDate, '"', sep='')), time_increment= 1  ,                        
                                        fields = toJSON(c("impressions",
                                                  "date_start",
                                                  "reach",
                                                  "frequency", 
                                                  "ad_name",
                                                  "adset_name", 
                                                  "campaign_name", 
                                                  "clicks",
                                                  "cpc",
                                                  "cpm",
                                                  "cpp",
                                                  "ctr",
                                                  "objective",
                                                  "spend", 
                                                  "account_name",
                                                  "ad_id",
                                                  "adset_id",
                                                  "buying_type",
                                                  "campaign_id",
                                                  "canvas_avg_view_time",
                                                  "cost_per_unique_click",
                                                  "inline_link_click_ctr",
                                                  "social_spend",
                                                  "total_action_value",
                                                  "unique_clicks",
                                                  "unique_ctr",
                                                  "website_purchase_roas")));


  FBInsightsExpanded <- list.stack(list.select(FBInsightsExpanded, website_purchase_roas = website_purchase_roas$value));

** 编辑:建议的包列表和相关代码位

标签: rdata.table

解决方案


我猜 Facebook 正在以列表格式为您提供 JSON?如果您想要一个数据框,您首先需要从列表中解压缩它,但这是与原始问题不同的主题。

就您最初的问题而言,使用 is_empty() 函数可以很简单地做到这一点purrr。目前尚不清楚原始示例中具有 NULL 值的列被称为什么,因此我将其称为value和数据框fb_data

干得好:

library(purrr)
library(tidyverse)

fb_data <- fb_data %>%
    mutate(value = ifelse(is_empty(value) == TRUE,NA,value))

这是您在此执行的操作:您正在检查 value 列中的行是否为空(即 NULL),如果为真,则将其更改为 NA,否则将不理会它。


推荐阅读