[R] Help with writing data to csv

Daniel Nordlund djnordlund at frontier.com
Sun May 13 00:33:28 CEST 2012


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of DL
> Sent: Saturday, May 12, 2012 1:33 PM
> To: r-help at r-project.org
> Subject: [R] Help with writing data to csv
> 
> 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/pract
> ice.csv",
> sep="\t", quote=F, row.names=F)
> 
>  write.csv(practice, file =
> "C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/pract
> ice.csv")
> 
> > write.csv(practice, file =
> >
> "C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/pract
> ice.csv",
> > row.names=FALSE)
> 
> write.csv(practice, file =
> "C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/pract
> ice.csv",
> sep = "\t", row.names=FALSE)
> Warning message:
> In write.csv(practice, file =
> "C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/pract
> ice.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.
> 

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



More information about the R-help mailing list