[Rd] read.csv behaviour

Ben Bolker bbolker at gmail.com
Wed Sep 28 14:19:22 CEST 2011


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



More information about the R-devel mailing list