将Data.Table写为csv文件 [英] Write a Data.Table as a csv file

查看:63
本文介绍了将Data.Table写为csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个data.table,其中的列中有列表值.以下是Dput:

I have a data.table that has list values within the columns. Below is the dput:

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

当您第一次看到它时看起来不错,但是如果您进一步查看它,这就是我要选择一列的样子:

This looks ok when you see it at first but if you look further into it, this is what it looks like when I want to select a column:

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

How do I change df2 to just be a normal dataframe where it doesn't have these extra values within a and b like this? I am trying to write this file to a csv but it will not allow me to because it is saying there are vectors as the values.

谢谢!

这是生成列表的代码:

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",然后遵循用法"部分:

Load the right package, look at its help page, search for "csv", follow the Usage section:

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

这篇关于将Data.Table写为csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆