2013-08-23 80 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