2013-08-23 77 views
-1

我正在寻找一种将类似表格的R对象导出到excel/csv的方法。例如增益表,在R中它被保存为一个对象。通常的write.table函数不允许我将对象转换为csv。尽管对象就像是一个表...:将R对象表导出为ex​​cel

增益(实际的,预测的,基团= 10,最优= FALSE,百分比= FALSE) - > gains_test2

> dput(head(gains_test2)) 

structure(list(depth = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 
100), obs = c(146L, 147L, 147L, 147L, 147L, 146L, 147L, 147L, 
147L, 147L), cume.obs = c(146L, 293L, 440L, 587L, 734L, 880L, 
1027L, 1174L, 1321L, 1468L), mean.resp = c(0.0116469582940705, 
0.0125322442801302, 0.0142252481339337, 0.0106531565074638, 0.0130847548479835, 
0.0106034244135535, 0.00455378455081303, 0.0061834241946857, 
0.00989105136990672, 0.00841145735618072), cume.mean.resp = c(0.0116469582940705, 
0.012091112014039, 0.0128041074904584, 0.0122654536667783, 0.012429537145848, 
0.0121265684425355, 0.0110426354025324, 0.0104341992461836, 0.010373758112336, 
0.0101772606932932), cume.pct.of.total = c(0.113817337305101, 
0.237124763149948, 0.377090015198269, 0.481908695451763, 0.610652390679108, 
0.714272022740551, 0.759077680710614, 0.819917710900762, 0.917237876073098, 
1)), .Names = c("depth", "obs", "cume.obs", "mean.resp", "cume.mean.resp", 
"cume.pct.of.total")) 


> print(gains_test2) 
Depth       Cume Cume Pct      Mean 
of   Cume  Mean  Mean of Total Lift Cume  Model 
File N  N  Resp  Resp  Resp  Index Lift  Score 
------------------------------------------------------------------------- 
10 146 146  0.01  0.01  11.38 %  114 114  0.02 
20 147 293  0.01  0.01  23.71 %  123 119  0.01 
30 147 440  0.01  0.01  37.71 %  140 126  0.01 
40 147 587  0.01  0.01  48.19 %  105 121  0.01 
50 147 734  0.01  0.01  61.07 %  129 122  0.01 
60 146 880  0.01  0.01  71.43 %  104 119  0.01 
70 147 1027  0.00  0.01  75.91 %  45 109  0.01 
80 147 1174  0.01  0.01  81.99 %  61 103  0.01 
90 147 1321  0.01  0.01  91.72 %  97 102  0.01 
100 147 1468  0.01  0.01 100.00 %  83 100  0.01 


> as.data.frame(gains_test2) 
Error in as.data.frame.default(gains_test2) : 
    cannot coerce class '"gains"' into a data.frame 

任何想法?谢谢!

============================================== ==========================

+12

你有没有签出'?write.csv'? – ialm

+3

R介绍可能重复。 –

+0

如果我真的需要将数据移动到Excel中,我使用'RODBC'包和'sqlSave'函数,否则我使用RExcel(http://www.statconn.com/products。 HTML)。 – Dinre

回答

0

这不是一个数据框,但它很容易把它变成一个。

dat <- as.data.frame(structure(list(depth = c(10, 20, 30, 40, 50, 60, 70, 80, 
90, 100), obs = c(146L, 147L, 147L, 147L, 147L, 146L, 147L, 147L, 
147L, 147L), cume.obs = c(146L, 293L, 440L, 587L, 734L, 880L, 
1027L, 1174L, 1321L, 1468L), mean.resp = c(0.0116469582940705, 
0.0125322442801302, 0.0142252481339337, 0.0106531565074638, 0.0130847548479835, 
0.0106034244135535, 0.00455378455081303, 0.0061834241946857, 
0.00989105136990672, 0.00841145735618072), cume.mean.resp = c(0.0116469582940705, 
0.012091112014039, 0.0128041074904584, 0.0122654536667783, 0.012429537145848, 
0.0121265684425355, 0.0110426354025324, 0.0104341992461836, 0.010373758112336, 
0.0101772606932932), cume.pct.of.total = c(0.113817337305101, 
0.237124763149948, 0.377090015198269, 0.481908695451763, 0.610652390679108, 
0.714272022740551, 0.759077680710614, 0.819917710900762, 0.917237876073098, 
1)), .Names = c("depth", "obs", "cume.obs", "mean.resp", "cume.mean.resp", 
"cume.pct.of.total"))) 
write.csv(dat, file="test.csv") 

显然成功。 (大多数R人会说,具有和不具有属性的原子和递归向量,现在包括列表,数据帧,矩阵,数组,普通向量和因子的伞都是“对象”。)

我不能告诉原始gains_test2对象除了结构对象中的列表之外还有更多东西。增益分类对象的打印方法似乎增加了额外的信息,这可能不在head返回的前六个列表中。我会认为,如果更不规则的是会有更多的属性,但也许这些复杂性在7个或更高的收益对象列表中。

您可能会发现可以使用head(gains_test2, 10)提取前10个列表并将其转换为数据框。我承认我有时会通过屏幕抓取R控制台输出并使用“数据/文本到列...”对话框来导出到Excel。如果你最终忘记输出的对象并发现对象的结构不是你在控制台上看到的,那么也有capture.console函数。

+0

谢谢!抱歉..第一次尝试将对象转换为data.frame。 – zhifff