如何将多个表,数据框,回归结果等写入一个Excel文件? [英] How to write multiple tables, dataframes, regression results etc - to one excel file?

查看:260
本文介绍了如何将多个表,数据框,回归结果等写入一个Excel文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将对象导入MS Excel的简便方法。
(我在示例中使用预装的 Puromycin-数据集)

I am looking for an easy way to get objects into MS Excel. (I am using the preinstalled "Puromycin"-dataset for the examples)

我想将这些对象的内容放置到单个excel文件中:

I would like to place the contents of these objects to a single excel file:

Puromycin
summary(Puromycin$rate)
summary(Purymycin$conc)
table(Puromycin$state)
lm( conc ~  rate , data=Puromycin)

内容是指按Enter键时控制台中显示的内容。我不知道该怎么称呼。

By "contents" i mean what is shown in the console when i press enter. I dont know what to call it.

我试图这样做:

sink("datafilewhichexcelhopefullyunderstands.csv")
Puromycin
summary(Puromycin$rate)
summary(Purymycin$conc)
table(Puromycin$state)
lm( conc ~  rate , data=Puromycin)
sink()

CSV扩展名的文件,但是当我在记事本中打开文件
时,逗号分隔。这意味着我无法让Excel正确打开它。适当地,
i表示每个数字都在其自己的单元格中。

This gives med a file with the CSV-extension, however when i open the file in notepad, there is comma-separation. That means that i cant get Excel to open it properly. By properly i mean that each number is in its own cell.

其他人针对类似的问题建议使用
https://stackoverflow.com/a/13007555/1831980

Others have suggested this for a similar problem https://stackoverflow.com/a/13007555/1831980

但作为新手,我觉得解决方案太复杂了,我希望有一个更简单的方法。

But as a novice i feel that the solution is too complex, and I am hoping for a simpler method.

我现在正在做的是:

write.table(Puromycin, file="clipboard" , sep=";" , row.names=FALSE ) 
write.table(summary(Purymycin$conc), file="clipboard" , sep=";" , row.names=FALSE )
... etc...

但这需要我进行大量复制和粘贴,我希望消除这些粘贴和粘贴。

But this requires i lot of copy-ing and pasting, which I hope to eliminate.

任何帮助将不胜感激。 / p>

Any help would appreciated.

推荐答案

write.table 及其朋友旨在写出以下内容的列数据由指定的任何分隔符分隔。剪贴板包含几种数据类型,因为您使用的是 summary ,它总是提供唯一的输出。

write.table and its friends are intended to write out columns of data separated by whatever separator is specified. Your clipboard contains several data types because you are using summary which always gives a unique output.

用于写入数据值,可以在数据框上使用 write.csv ,然后使用Excel打开。例如, Puromycin 已经是一个数据框(您可以通过 str(Puromycin)看到),因此您只需直接写出来:

For writing the data values out, you can use write.csv on a data frame and then open with Excel. For example, Puromycin is already a data frame (which you can see with str(Puromycin)) so you can just write it out directly:

write.csv(file = "some file.csv", x = Puromycin)

这将进入当前工作目录(可以通过 getwd()确定)。

Which will go into the current working directory (which can be determined with getwd()).

写出/保存回归模型的结果还有些挑战。您绝对可以像以前一样使用 sink ,但是在文件上指定 .txt 的扩展名,以便文本编辑器可以打开它。从长远来看,您可能想要研究更高级的方法( sweave knitr ),因为它们可以自动编写非常好的报告。

To write out/save the results of the regression model is a bit more of a challenge. You could definitely use sink as you did, but specify an extension of .txt on your file so a text editor can open it. There are fancier methods (sweave, knitr) which you might want to look into in the long run, as they can write really nice reports automatically.

与此同时,了解 str(任何R对象)成为你的朋友。您可以使用 ls()查看工作区中的所有对象。

In the meantime, get to know str(any R object) as it will be your friend. You can see all the objects in your workspace with ls().

这篇关于如何将多个表,数据框,回归结果等写入一个Excel文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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