Michael Reinecke
2006-Feb-01 11:31 UTC
[R] Write.table: change points to commas when object contains a row of characters
Dear Group! I asked write.table to change the decimal point from "." to "," , but apparently it would only do so if the object to be written does not contain any character elements. I would like to understand, why this has to be so and - of course - find a solution for my matrix object jjmat, that I tried to write out by write.table(jjmat, file="jjmat.txt", row.names=TRUE, col.names=NA,sep="\t",dec=",") I also tried "options(OutDec=",")" , which changes the presentation on the console, but seems to have no influence on write table: jjmat is still written out with points instead of commas. The object looks like this:> jjmatf2a1 f2b1 f5a1 f5b1 f5c1 rowname1 "coltext1" "coltext2" "coltext3" "coltext4" "coltext5" rowname2 4,428571 4,326531 4,265306 3,959184 3,306122 rowname3 0,469665 0,3328301 0,1776079 -0,1758072 0,0870965 rowname4 4,275862 4,206897 4,137931 3,931034 3,379310> deparse(jjmat)[1] "structure(list(\"coltext1\", 4.42857142857143, 0.469664970752337, " [2] " 4.27586206896552, \"coltext2\", 4.3265306122449, 0.332830055973803, " [3] " 4.20689655172414, \"coltext3\", 4.26530612244898, 0.177607859264292, " [4] " 4.13793103448276, \"coltext4\", 3.95918367346939, -0.175807245137424, " [5] " 3.93103448275862, \"coltext5\", 3.30612244897959, 0.087096493847482, " [6] " 3.37931034482759), .Dim = c(4, 5), .Dimnames list(c(\"rowname1\", " [7] "\"rowname2\", \"rowname3\", \"rowname4\"), c(\"f2a1\", \"f2b1\", \"f5a1\", " [8] "\"f5b1\", \"f5c1\")))" Do I have to change the structure of jjmat? Thanks for your comments! Greetings, Michael [[alternative HTML version deleted]]
Prof Brian Ripley
2006-Feb-01 15:24 UTC
[R] Write.table: change points to commas when object contains a row of characters
You cannot have a matrix or a data frame which is partially numeric and partially character (within a column for a data frame). You seem rather to have a list matrix. Then ?write.table does say Any columns in a data frame which are lists or have a class (e.g. dates) will be converted by the appropriate 'as.character' method: such columns are unquoted by default. On the other hand, any class information for a matrix is discarded. Although it does not say so, the same happens with a matrix. You need to get a table before you try outputting it: I suggest using format() to help you. On Wed, 1 Feb 2006, Michael Reinecke wrote:> Dear Group! I asked write.table to change the decimal point from "." to > "," , but apparently it would only do so if the object to be written > does not contain any character elements. I would like to understand, why > this has to be so and - of course - find a solution for my matrix object > jjmat, that I tried to write out by > > > > write.table(jjmat, file="jjmat.txt", row.names=TRUE, > col.names=NA,sep="\t",dec=",") > > > > I also tried "options(OutDec=",")" , which changes the presentation on > the console, but seems to have no influence on write table: jjmat is > still written out with points instead of commas. > > > > The object looks like this: > > > >> jjmat > > f2a1 f2b1 f5a1 f5b1 f5c1 > > rowname1 "coltext1" "coltext2" "coltext3" "coltext4" "coltext5" > > rowname2 4,428571 4,326531 4,265306 3,959184 3,306122 > > rowname3 0,469665 0,3328301 0,1776079 -0,1758072 0,0870965 > > rowname4 4,275862 4,206897 4,137931 3,931034 3,379310 > > > >> deparse(jjmat) > > [1] "structure(list(\"coltext1\", 4.42857142857143, 0.469664970752337, " > > > [2] " 4.27586206896552, \"coltext2\", 4.3265306122449, > 0.332830055973803, " > > [3] " 4.20689655172414, \"coltext3\", 4.26530612244898, > 0.177607859264292, " > > [4] " 4.13793103448276, \"coltext4\", 3.95918367346939, > -0.175807245137424, " > > [5] " 3.93103448275862, \"coltext5\", 3.30612244897959, > 0.087096493847482, " > > [6] " 3.37931034482759), .Dim = c(4, 5), .Dimnames > list(c(\"rowname1\", " > > [7] "\"rowname2\", \"rowname3\", \"rowname4\"), c(\"f2a1\", \"f2b1\", > \"f5a1\", " > > [8] "\"f5b1\", \"f5c1\")))" > > > > > > Do I have to change the structure of jjmat? Thanks for your comments! > > > > Greetings, > > > > Michael > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Michael Reinecke
2006-Feb-01 17:30 UTC
[R] Write.table: change points to commas when object contains a row of characters
Thank you very much! I wonder why I did not yet come across that function format(). Guess this won ??t be the last time that I use it. The following did exactly what I was looking for: temp<-attributes(jjmat) jjmat<-format(jjmat, decimal.mark=",") attributes(jjmat)<-temp With these changes jjmat was perfect for export to excel. -----Urspr??ngliche Nachricht----- Von: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] Gesendet: Mittwoch, 1. Februar 2006 16:31 An: Michael Reinecke Cc: R-help at stat.math.ethz.ch Betreff: Re: [R] Write.table: change points to commas when object contains a row of characters You cannot have a matrix or a data frame which is partially numeric and partially character (within a column for a data frame). You seem rather to have a list matrix. Then ?write.table does say Any columns in a data frame which are lists or have a class (e.g. dates) will be converted by the appropriate 'as.character' method: such columns are unquoted by default. On the other hand, any class information for a matrix is discarded. Although it does not say so, the same happens with a matrix. You need to get a table before you try outputting it: I suggest using format() to help you. On Wed, 1 Feb 2006, Michael Reinecke wrote:> Dear Group! I asked write.table to change the decimal point from "." > to "," , but apparently it would only do so if the object to be > written does not contain any character elements. I would like to > understand, why this has to be so and - of course - find a solution > for my matrix object jjmat, that I tried to write out by > > > > write.table(jjmat, file="jjmat.txt", row.names=TRUE, > col.names=NA,sep="\t",dec=",") > > > > I also tried "options(OutDec=",")" , which changes the presentation on > the console, but seems to have no influence on write table: jjmat is > still written out with points instead of commas. > > > > The object looks like this: > > > >> jjmat > > f2a1 f2b1 f5a1 f5b1 f5c1 > > rowname1 "coltext1" "coltext2" "coltext3" "coltext4" "coltext5" > > rowname2 4,428571 4,326531 4,265306 3,959184 3,306122 > > rowname3 0,469665 0,3328301 0,1776079 -0,1758072 0,0870965 > > rowname4 4,275862 4,206897 4,137931 3,931034 3,379310 > > > >> deparse(jjmat) > > [1] "structure(list(\"coltext1\", 4.42857142857143, 0.469664970752337, " > > > [2] " 4.27586206896552, \"coltext2\", 4.3265306122449, > 0.332830055973803, " > > [3] " 4.20689655172414, \"coltext3\", 4.26530612244898, > 0.177607859264292, " > > [4] " 4.13793103448276, \"coltext4\", 3.95918367346939, > -0.175807245137424, " > > [5] " 3.93103448275862, \"coltext5\", 3.30612244897959, > 0.087096493847482, " > > [6] " 3.37931034482759), .Dim = c(4, 5), .Dimnames > list(c(\"rowname1\", " > > [7] "\"rowname2\", \"rowname3\", \"rowname4\"), c(\"f2a1\", \"f2b1\", > \"f5a1\", " > > [8] "\"f5b1\", \"f5c1\")))" > > > > > > Do I have to change the structure of jjmat? Thanks for your comments! > > > > Greetings, > > > > Michael > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595