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.
write.tableand 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 usingsummarywhich always gives a unique output.For writing the data values out, you can use
write.csvon adata frameand then open with Excel. For example,Puromycinis already a data frame (which you can see withstr(Puromycin)) so you can just write it out directly: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
sinkas you did, but specify an extension of.txton 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 withls().