[R] Writing data frames

Simon Fear Simon.Fear at synequanon.com
Tue Dec 30 11:25:35 CET 2003


Another way in this particular case is to transpose m:

write.table(t(m), file="", row.names=FALSE,quote=FALSE)

This transposition creates a 1x2 matrix, for which there is
a method (as.data.frame.matrix). (Don't just use as.matrix(m),
that will be a 2x1 matrix.)

Note that there is a missing dimension spec in the structure of 
the array object m - contrast str(m) with str(t(m)). Arrays exist to 
be ragged - so there's no default coercion method to the 
non-ragged data.frame class.

You could also skip all of the above and work with sink() instead
of write.table().


> -----Original Message-----
> From: Andy Bunn [mailto:abunn at montana.edu]
> Sent: 30 December 2003 00:44
> To: 'Ton van Daelen'; r-help at stat.math.ethz.ch
> Subject: RE: [R] Writing data frames
> 
> 
> Security Warning: 
> If you are not sure an attachment is safe to open please contact  
> Andy on x234. There are 0 attachments with this message. 
> ________________________________________________________________ 
>  
> The key is in the error message:
> "...can't coerce array into a data.frame"
> 
> Even if as.data.frame.default is unhappy you can coerce m into a
> data.frame.
> 
> write.table(data.frame(m1 = m[1], m2 = m[2]), file="C:\\R\\tst.txt",
> col.names=T, row.names=F, quote=F, append = FALSE)
> 
> There's probably a nicer way to do the coercion but this works.
> 
> HTH, Andy
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>  
 
Simon Fear 
Senior Statistician 
Syne qua non Ltd 
Tel: +44 (0) 1379 644449 
Fax: +44 (0) 1379 644445 
email: Simon.Fear at synequanon.com 
web: http://www.synequanon.com 
  
Number of attachments included with this message: 0 
  
This message (and any associated files) is confidential and\...{{dropped}}




More information about the R-help mailing list