Hi there - I have been trying to generate some simple stats and save the results to a file. My data looks like this: x y z exp 0 3 5 1 2 11 10 1 4 4 5 1 7 6 4 1 11 1 2 2 5 7 1 2 3 3 3 2 1 6 1 2 and the script I ran is: ========== data <- read.table("c:\\R\\data.txt", header=T) attach(data) m <- tapply(x, exp, mean) write.table(m, file="c:\\R\\tst.txt", col.names=T, row.names=F, quote=F, append = FALSE) =========== I'd like to write the full contents of m: 1 2 3.25 5.00 to a file, but the write.table call gives me an error: Error in as.data.frame.default(x[[i]], optional = TRUE) : can't coerce array into a data.frame Can anyone tell me what the most convenient/flexible way is to write data.frames? Thanks - Ton Ton van Daelen, PhD Director, Application Support Tel: (858) 279-8800 ext 217 Fax: (858) 279-8804 Web: www.scitegic.com SciTegic Inc. - ask more of your data [[alternative HTML version deleted]]
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
Try this with the m in your example: write.table(t(m), file="c:/R/tst.txt", col.names=names(m), row.names=F, quote=F, append=F) --- Date: Mon, 29 Dec 2003 16:21:15 -0800 From: Ton van Daelen <tvandaelen at scitegic.com> To: <r-help at stat.math.ethz.ch> Subject: [R] Writing data frames Hi there - I have been trying to generate some simple stats and save the results to a file. My data looks like this: x y z exp 0 3 5 1 2 11 10 1 4 4 5 1 7 6 4 1 11 1 2 2 5 7 1 2 3 3 3 2 1 6 1 2 and the script I ran is: ========== data <- read.table("c:\\R\\data.txt", header=T) attach(data) m <- tapply(x, exp, mean) write.table(m, file="c:\\R\\tst.txt", col.names=T, row.names=F, quote=F, append = FALSE) =========== I'd like to write the full contents of m: 1 2 3.25 5.00 to a file, but the write.table call gives me an error: Error in as.data.frame.default(x[[i]], optional = TRUE) : can't coerce array into a data.frame Can anyone tell me what the most convenient/flexible way is to write data.frames? Thanks - Ton Ton van Daelen, PhD Director, Application Support Tel: (858) 279-8800 ext 217 Fax: (858) 279-8804 Web: www.scitegic.com SciTegic Inc. - ask more of your data
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}}