将带有列表的数据框写入CSV

时间:2018-10-14 08:16:04

标签: r

我正在使用https://cran.r-project.org/web/packages/fbRads/fbRads.pdf从facebook marketing API导入数据。

https://developers.facebook.com/docs/marketing-api/reference/adgroup/insights/-website_purchase_roas字段之一是字段列表。

如何将其放入CSV文件中?

 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

到目前为止,我一直在使用

write.table(get(fileName),paste0(“ / home / rstudio /”,fileName),append = TRUE,na =“ NA”,row.names = FALSE,sep =“,”,列名= FALSE)

但是,这当然不能处理列表,我需要先展平数据框。

1 个答案:

答案 0 :(得分:0)

您可以使用fwrite程序包中的data.table,该程序包具有一个sep2参数,该参数确定将什么用作list列的分隔符。

请参见?data.table::fwrite

示例:

library(data.table)
set.seed(01349)
DT = data.table(
  V1 = rchisq(5, 5), 
  l_col = lapply(integer(5L), function(x) rnorm(sample(3L, 1L)))
)

fwrite(DT)
# V1,l_col
# 8.55552022121412,-0.865231813182414
# 1.24426281601847,-0.193327839843266|0.215528624259188|0.347253218410811
# 3.43283896352093,0.87954526007159
# 7.60971421627405,-2.37928159590364
# 8.83757675994184,-0.981183793508651|-1.29924540435512