From bbolker at gmail.com  Wed Sep 28 14:19:22 2011
From: bbolker at gmail.com (Ben Bolker)
Date: Wed, 28 Sep 2011 12:19:22 +0000
Subject: [Rd] read.csv behaviour
References: <3CBFCFB1FEFFA841BA83ADF2F2A9C6FA01AB2673@mango-data1.Mango.local>
Message-ID: <loom.20110928T141727-917@post.gmane.org>

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



[Rd] read.csv behaviour
Ben Bolker