Skip to content

Help with writing data to csv

8 messages · DL, michael.weylandt at gmail.com (R. Michael Weylandt, Daniel Nordlund +4 more

DL
#
I am trying to write data to csv but I am having issues with the separations. 

Basically I have some results that I get with R that I copied and pasted
into word and then saved as .txt 

I want to write the results to csv because it's easier to make tables in
word (all I would have to do is copy and paste into a table, instead of
typing everything out). 

I am able to write the data to the .csv file but the data is not
comma-delimited when I open the file. Everything is written into the first
cell. 

These are the various commands that I have been inputting:

write.csv(practice, file.choose(new=T), quote=F, row.names=F)

write.csv(practice, file.choose(new=T), sep=",", quote=F, row.names=F)
Warning message:
In write.csv(practice, file.choose(new = T), sep = ",", quote = F,  :
  attempt to set 'sep' ignored

write.table(practice, file =
"C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/practice.csv",
sep="\t", quote=F, row.names=F)

 write.csv(practice, file =
"C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/practice.csv")
write.csv(practice, file =
"C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/practice.csv",
sep = "\t", row.names=FALSE)
Warning message:
In write.csv(practice, file =
"C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/practice.csv", 
:
  attempt to set 'sep' ignored


As you can see, the commands in which I write to csv and set "sep" I get a
message saying that attempt to set "sep" ignored. All the other commands
work but I don't get a comma-delimited nor a tab-delimited file.

Am I doing something wrong?

Thank you. 

--
View this message in context: http://r.789695.n4.nabble.com/Help-with-writing-data-to-csv-tp4629436.html
Sent from the R help mailing list archive at Nabble.com.
By definition a csv is comma-separated so you can't set a separator. The general function which you seem to be seeking is write.table

Michael
On May 12, 2012, at 3:33 PM, DL <chombito718 at hotmail.com> wrote:

            
#
Well, the function write.csv() is a wrapper for write.table() and it means just what it says, write CSV (i.e. Comma Separated Value), so you can't change the separator.  If you want to use a tab separator, then you need to use write.table() directly.  

That being said, what was the result of the output of the very first command that you said you used?

write.csv(practice, file.choose(new=T), quote=F, row.names=F)

I used the built-in data frame, cars, instead of your file practice and it worked just fine in producing a comma separated value file.

Dan

Daniel Nordlund
Bothell, WA USA
DL
#
Thank you for replying. The result of the first command was an excel sheet
with the data separated by rows but not columns, which is basically what I
get when I copy and paste into Excel. I would like to have the information
separated by rows and columns so I can just copy and paste it into a table
in word and not have to type the data. I'm not sure if I'm being clear. 

--
View this message in context: http://r.789695.n4.nabble.com/Help-with-writing-data-to-csv-tp4629436p4629724.html
Sent from the R help mailing list archive at Nabble.com.
#
I have had no problem at all with writing CSV files and copy/pasting
the data into WORD.  What you really need to show is what is the
actual output in the file that you are working with, and then exactly
what you are doing with the data in WORD.  So I know that it works
fine, so it must be "pilot error".
On Sat, May 12, 2012 at 7:24 PM, DL <chombito718 at hotmail.com> wrote:

  
    
#
On May 12, 2012, at 7:24 PM, DL wrote:

            
That last sentence is very pertinent. This is a mailing list and not a  
website, regardless of what you  might be "seeing". Many of us have  
actually read the Posting Guide.
No. You sent it from Nabble, and it was forwarded to the R mailing  
list .... and it has no context. And Nabble is NOT the rhelp archive.
#
On Sat, 12 May 2012 13:33:19 -0700 (PDT)
DL <chombito718 at hotmail.com> wrote:

            
...
I'm not sure what all you are doing wrong, but the problems are limited
to R.  You don't seem understand, Word, Excel, or how to use csv files
either.

Since you don't actually provide an example what "practice" is, it is
not clear just what the problem might be.  If perchance "Practice" is
the result of running a routine on data and capturing the result, then
you might want to try ?sink(), which, properly instructed, will write
the "results" to a file.  If that is the case, you might also want to
consider what fonts you are using in Word.

However, without providing the list with an example of Practice, we're
left to guess precisely what kind of help you really need.

JWD
1 day later
#
If you have a data.frame and you want a table in Microsoft Word, the
quickest path (without additional packages and assuming you are using
Windows) is

write.table(DataFrameName, file="clipboard", sep="\t", row.names=FALSE)
# If the file is large, you may need "clipboard-128" instead of "clipboard."

Now open Microsoft Excel and select Paste. You now have the data.frame in
Excel. Select the data and copy it. Now open Microsoft Word and select
Paste. You now have the data.frame in Word as a table.

If you need something more flexible, look at packages xlsx, xlsReadWrite,
and R2wd.

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352