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", > row.names=FALSE)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.
R. Michael Weylandt <michael.weylandt@gmail.com>
2012-May-12 22:17 UTC
[R] Help with writing data to csv
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:> 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", >> row.names=FALSE) > > 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. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
> -----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
Take a look at the data in a text file and you will see that it is a CSV file with a single column: "1 ONE.LEVEL.ANALYSIS.WITH.Speaker..random..and.folseg..5.04e.08....preseg..3.77e.07....position..0.402....sylstress..0.93. " "2 $preseg " "3 factor coef tokens mean " "4 s 0.128 1 1.075 " "5 i 0.028 50 1.040 " "6 e -0.009 27 1.001 " "7 z -0.009 3 1.036 " "8 a -0.018 52 0.996 " "9 o -0.057 23 0.960 " "10 u -0.064 10 0.942 " "11 $folseg " "12 factor coef tokens mean " "13 i 0.063 50 1.041 " "14 e 0.060 19 1.026 " "15 s 0.007 8 1.001 " "16 a 0.005 61 0.996 " It appears that you have data stored in a list, and not a dataframe, that you are calling write.csv with. Show the actual write.csv file and the 'str' of the object that you are trying to write. Also your data has varying number of columns. Exactly what were you trying to do? The system is doing exactly what you are telling it to do. So what I would need to see is the actual object you are working with and then what you expect to see as output. On Sat, May 12, 2012 at 9:10 PM, Delano L <chombito718 at hotmail.com> wrote:> Hi Jim, > > Thank you for responding. I wanted to reply to you in the forum but I'm not > able to access it due to database being updated. I hope I am not offending > you by replying directly to your email. > > This is the information that I'm trying to write to a csv file: > > The attached file is what I get. You'll see that it's separated by rows but > not by columns. > Thanks for your help. > Delano > > ONE-LEVEL ANALYSIS WITH Speaker [random] and folseg (5.04e-08) + preseg > (3.77e-07) + position (0.402) + sylstress (0.93) > $preseg > ?factor?? coef tokens? mean > ????? s? 0.128????? 1 1.075 > ????? i? 0.028???? 50 1.040 > ????? e -0.009???? 27 1.001 > ????? z -0.009????? 3 1.036 > ????? a -0.018???? 52 0.996 > ????? o -0.057???? 23 0.960 > ????? u -0.064???? 10 0.942 > > $folseg > ?factor?? coef tokens? mean > ????? i? 0.063???? 50 1.041 > ????? e? 0.060???? 19 1.026 > ????? s? 0.007????? 8 1.001 > ????? a? 0.005???? 61 0.996 > ????? u -0.040????? 2 0.944 > ????? z -0.041???? 25 0.939 > ????? o -0.054????? 1 0.871 > > $position > ?factor?? coef tokens? mean > ????? i? 0.011???? 52 1.013 > ????? f -0.002???? 68 0.992 > ????? m -0.009???? 46 1.008 > > $sylstress > ?factor?? coef tokens? mean > ????? a? 0.001??? 104 1.008 > ????? t -0.001???? 62 0.995 > > $Speaker > ? random coef tokens? mean std dev > ?part005??? 0???? 33 1.002?????? 0 > ?part017??? 0???? 55 1.004?????? 0 > ?part018??? 0???? 27 1.001?????? 0 > ?part019??? 0???? 51 1.004?????? 0 > > $misc > ?deviance df intercept grand mean > ?-469.687 18???? 0.991????? 1.003 > > > >> Date: Sat, 12 May 2012 20:20:38 -0400 >> Subject: Re: [R] Help with writing data to csv >> From: jholtman at gmail.com >> To: chombito718 at hotmail.com >> CC: r-help at r-project.org >> >> 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: >> > 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. >> > >> > ______________________________________________ >> > R-help at r-project.org mailing list >> > https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guide >> > http://www.R-project.org/posting-guide.html >> > and provide commented, minimal, self-contained, reproducible code. >> >> >> >> -- >> Jim Holtman >> Data Munger Guru >> >> What is the problem that you are trying to solve? >> Tell me what you want to do, not how you want to do it.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
On Sat, 12 May 2012 13:33:19 -0700 (PDT) DL <chombito718 at hotmail.com> wrote:> 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'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
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> -----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 3: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/pr > actice.csv", > sep="\t", quote=F, row.names=F) > > write.csv(practice, file > "C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/pr > actice.csv") > > > write.csv(practice, file > > > "C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/pr > actice.csv", > > row.names=FALSE) > > write.csv(practice, file > "C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/pr > actice.csv", > sep = "\t", row.names=FALSE) > Warning message: > In write.csv(practice, file > "C:/Users/User/Documents/Documents/Proyectodeafroantillanos/Laterals/pr > actice.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. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.