[Rd] csv version of data in an R object

Max Kuhn mxkuhn at gmail.com
Sat Apr 21 16:28:25 CEST 2012


For a package, I need to write a csv version of a data set to an R
object. Right now, I use:

    out <- capture.output(
                          write.table(x,
                                      sep = ",",
                                      na = "?",
                                      file = "",
                                      quote = FALSE,
                                      row.names = FALSE,
                                      col.names = FALSE))

To me, this is fairly slow; 131 seconds for a data frame with 8100
rows and 1400 columns.

The data will be in a data frame; I know write.table() would be faster
with a matrix. I was looking into converting the data frame to a
character matrix using as.matrix() or, better yet, format() prior to
the call above. However, I'm not sure what an appropriate value of
'digits' should be so that the character version of numeric data has
acceptable fidelity.

I also tried using a text connection and sink() as shown in
?textConnection but there was no speedup.

Any suggestions or ideas?

Thanks,

Max


> sessionInfo()
R version 2.14.0 (2011-10-31)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.14.0



More information about the R-devel mailing list