[R] Arbirtrary column names with write.csv

Rafael Laboissiere rafael.laboissiere at inserm.fr
Tue Mar 3 15:42:52 CET 2009

* Uwe Ligges <ligges at statistik.tu-dortmund.de> [2009-03-03 15:27]:

> Rafael Laboissiere wrote:
>> Is there a way to prevent write.csv to transform the column names a la
>> make.names?  I mean, if I write the code:
>>     x <- structure (NULL)
>>     x [["a+b"]] <- c (1,2)
>>     write.csv (x, file = "foo.csv", row.names = FALSE, quote = FALSE)
> your x is not a data.frame, hence write.csv() (or better, write.table())  
> converts your *list* x to a data.frame implicitly by using as.data.frame  
> (and this tries to convert names to more regular names by make.names()).  
> You can avoid it by either working on data.frames or stop the  
> make.names() conversion by forcing the data.frame in advance as in:
>      write.csv (as.data.frame(x, optional=TRUE), file = "foo.csv",  
>                 row.names = FALSE, quote = FALSE)

Great, it works.  Thanks!

Rafael Laboissiere

More information about the R-help mailing list