This might be obvious but I was wondering if anyone knows quick and easy way of writing out a CSV file with varying row lengths, ideally an initial data read from a CSV file which has the same format. See example below. I found it quite strange that R cannot write it in one go, so one must append blocks or post-process the file, is this true? (even Ruby can do it!!) Otherwise it puts ,"","" or similar for missing column values in the shorter length rows and fill=FALSE option do not work! I don't want to post-process if possible. See this post: http://r.789695.n4.nabble.com/Re-read-csv-trap-td3301924.html Example that generated Error! writeLines(c("A,B,C,D", "1,a,b,c", "2,f,g,c", "3,a,i,j", "4,a,b,c", "5,d,e,f", "6,g,h,i,j,k,l,m,n"), con=file("test.csv")) read.csv("test.csv") try(read.csv("test.csv",fill=FALSE)) LEGAL NOTICE This message is intended for the use o...{{dropped:10}}
The consensus is that it is a very bad idea for a file nominally described as a CSV file to have a varying numbers of fields from record to record. If you have need for such a file and go to whatever lengths you need to in order to create it, please mark it as something other than a CSV file when you are done. With this in mind, you may be in a better position to understand why the pre-built facilities do not provide much support in your endeavor. --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Mehmet Suzen <msuzen@mango-solutions.com> wrote: This might be obvious but I was wondering if anyone knows quick and easy way of writing out a CSV file with varying row lengths, ideally an initial data read from a CSV file which has the same format. See example below. I found it quite strange that R cannot write it in one go, so one must append blocks or post-process the file, is this true? (even Ruby can do it!!) Otherwise it puts ,"","" or similar for missing column values in the shorter length rows and fill=FALSE option do not work! I don't want to post-process if possible. See this post: http://r.789695.n4.nabble.com/Re-read-csv-trap-td3301924.html Example that generated Error! writeLines(c("A,B,C,D", "1,a,b,c", "2,f,g,c", "3,a,i,j", "4,a,b,c", "5,d,e,f", "6,g,h,i,j,k,l,m,n"), con=file("test.csv")) read.csv("test.csv") try(read.csv("test.csv",fill=FALSE)) LEGAL NOTICE\ This message is intended for the use o...{...{{dropped:14}}
On 09/28/2011 09:23 AM, Mehmet Suzen wrote:> > This might be obvious but I was wondering if anyone knows quick and easy > way of writing out a CSV file with varying row lengths, ideally an > initial data read from a CSV file which has the same format. See example > below. > > > I found it quite strange that R cannot write it in one go, so one must > append blocks or post-process the file, is this true? (even Ruby can do > it!!) > > Otherwise it puts ,"","" or similar for missing column values in the > shorter length rows and fill=FALSE option do not work! > > I don't want to post-process if possible. > > See this post: > http://r.789695.n4.nabble.com/Re-read-csv-trap-td3301924.html > > Example that generated Error! > > writeLines(c("A,B,C,D", > "1,a,b,c", > "2,f,g,c", > "3,a,i,j", > "4,a,b,c", > "5,d,e,f", > "6,g,h,i,j,k,l,m,n"), > con=file("test.csv")) > > read.csv("test.csv") > try(read.csv("test.csv",fill=FALSE))Hi Mehmet, The example doesn't need to call "file", writeLines does it for you. It worked for me: writeLines(c("A,B,C,D", "1,a,b,c", "2,f,g,c", "3,a,i,j", "4,a,b,c", "5,d,e,f", "6,g,h,i,j,k,l,m,n"), con="test.csv") and to get the original object back, use: readLines("test.csv") The reason you can't use read.csv is that it returns a data frame, and that object can't have elements of unequal length. If you want an object with elements of unequal length, try: as.list(readLines("test.csv")) Jim
Mehmet Suzen <msuzen <at> mango-solutions.com> writes:> This might be obvious but I was wondering if anyone knows quick and easy > way of writing out a CSV file with varying row lengths, ideally an > initial data read from a CSV file which has the same format. See example > below. > > writeLines(c("A,B,C,D", > "1,a,b,c", > "2,f,g,c", > "3,a,i,j", > "4,a,b,c", > "5,d,e,f", > "6,g,h,i,j,k,l,m,n"), > con=file("test.csv")) >X <- read.csv("test.csv") It's not that pretty, but something like tmpf <- function(x) paste(x[nzchar(x)],collapse=",") writeLines(apply(as.matrix(X),1,tmpf),con="outfile.csv") might work