尝试在NULL上设置属性

时间:2018-10-12 06:42:50

标签: r data.table

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

**编辑:建议的包装清单和相关代码

1 个答案:

答案 0 :(得分:0)

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

对于您的原始问题,有一种非常简单的方法可以使用purrr中的is_empty()函数来完成。目前尚不清楚在您的原始示例中具有NULL值的列是什么名称,因此我将其称为 value 和数据帧 fb_data

您在这里:

library(purrr)
library(tidyverse)

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

这就是您要执行的操作:您正在检查value列中的行是否为空(即NULL),如果为true,则将其更改为NA,否则将其保留。