将 Data.Table 写入 csv 文件

Write a Data.Table as a csv file

我有一个 data.table,它的列中有列表值。下面是输出:

dput(df2)

structure(list(a = list(structure(5594.05118603497, .Names = "a"), 
structure(8877.42723091876, .Names = "a"), structure(2948.95666065332, 
.Names = "a"), 
structure(5312.77623937465, .Names = "a"), structure(676.637044992807, 
.Names = "a"), 
structure(323.104243007498, .Names = "a")), b = 
list(structure(3.90258318853593e-06, .Names = "b"), 
structure(3.89772483584672e-06, .Names = "b"), structure(3.91175458242421e- 
06, .Names = "b"), 
structure(3.90169532031545e-06, .Names = "b"), structure(6.54536728417568e- 
06, .Names = "b"), 
structure(6.59087917747312e-06, .Names = "b")), id = 1:6), .Names = c("a", 
"b", "id"), class = c("data.table", "data.frame"), row.names = c(NA, 
-6L), .internal.selfref = <pointer: 0x0000000000220788>)

输出结果如下:

head(df2)

          a            b id
1: 5594.051 3.902583e-06  1
2: 8877.427 3.897725e-06  2
3: 2948.957 3.911755e-06  3
4: 5312.776 3.901695e-06  4
5:  676.637 6.545367e-06  5
6: 323.1042 6.590879e-06  6

当你第一次看到它时看起来不错,但如果你进一步观察它,这就是我想要 select 专栏时的样子:

如何将 df2 更改为普通数据帧,其中 a 和 b 中没有像这样的这些额外值?我正在尝试将此文件写入 csv,但它不允许我这样做,因为它说有向量作为值。

谢谢!

编辑:

这是生成列表的代码:

test<-sapply(  split( df , df$ID), 
function(d){ dat <- list2env(d)
nlsfit <- nls( form = y ~ a * (1-exp(-b * x)), data=dat, 
start= list( a=max(dat$y), b=b.start),
           control= control1) 

list(a = coef(nlsfit)[1], b = coef(nlsfit)[2])} )
df1<-as.data.frame(t(test))

加载正确的包,查看其帮助页面,搜索 "csv",按照用法部分:

library(data.table)
help(pac=data.table)
fwrite(df2, file="~/test.csv") # for mac, need changing for other OS

另一种方法可能是:

 as.data.frame( lapply(df2, unlist) )