[Rd] write.table inconsistency (PR#7403)

From: <murdoch_at_stats.uwo.ca>
Date: Sat 04 Dec 2004 - 11:55:26 EST


There's an as.matrix() call in write.table that means the formatting of numeric columns changes depending on whether there are any non-numeric columns in the table or not. For example,

> x <- data.frame(a=1:10,b=1:10)
> write.table(x,sep=',',row.names=F)

"a","b"

1,1
2,2
3,3
4,4
5,5
6,6
7,7
8,8
9,9

10,10
> x <- data.frame(a=1:10,b=as.factor(1:10))
> write.table(x,sep=',',row.names=F)

"a","b"
 1,"1"
 2,"2"
 3,"3"
 4,"4"
 5,"5"
 6,"6"
 7,"7"
 8,"8"
 9,"9"
10,"10"

Notice that column "a" has a leading space in the second example, but not the first.

Normally this won't matter, but RSQLite uses write.table in the sqliteWriteTable function, which would cause column "a" above to be treated as text rather than numeric, and be sorted as text rather than into numerical order.

Duncan Murdoch



R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat Dec 04 12:00:29 2004

This archive was generated by hypermail 2.1.8 : Fri 18 Mar 2005 - 09:01:57 EST