How to write multiple tables, dataframes, regression results etc - to one excel file?

你。 提交于 2020-01-24 12:33:07

问题


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

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)

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

I tried to do this:

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

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.

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.

What I am doing now is this:

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.

Any help would appreciated.


回答1:


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.

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)

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

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.

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




回答2:


This will only be helpful if you are prepared to use Excel's Data/Text to Columns functions:

capture.output( sapply( c(Puromycin,
summary(Puromycin$rate),
summary(Puromycin$conc),
table(Puromycin$state),
lm( conc ~  rate , data=Puromycin) ), FUN=print), file="datafilewhichexcelhopefullyunderstands.csv", append=TRUE)

The problem being that Excel will not read the whitespace as a cell separator unless you specifically tell it to. You can (and I have often done so) use the fixed filed input features offered by the Text-to-Columns dialog interface.




回答3:


Your simplest option may be to use the RExcel tool, it transfers information between R and Excel. However it is not free software.

The XLConnect package is another option, it can be used to write information directly to an Excel file.

The tricky part is the lm call. lm does not return a simple vector, matrix, or data frame (all of which are easy to convert to csv or send directly) and there is not a clear way to convert the various parts of a list to cells in a spreadsheet. What would be better is to use extractor functions to pull the important parts from the return of lm or the summary of the lm object and send those to Excel using the other tools.

If you can tell us more about why you want the numbers in Excel and what you plan to do with them after, then we may be able to offer better help (you may be able to completely skip excel).

If the main goal is to share output with others then you should really look at the knitr package (or other related packages). This will not create Excel files, but can be used (along with the pandoc program and possibly other tools) to create a report file in a format easy to share with others not familiar with R. You could put everything into a .pdf file or a .docx file (the latter read by MS Word and would have tables wich can be edited using Word). There is not a simple way to get edits back into R, but with the track changes you can easily see what changes have been made and hand edit your R script/template accordingly.



来源:https://stackoverflow.com/questions/14444277/how-to-write-multiple-tables-dataframes-regression-results-etc-to-one-excel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!