Hi, I am using the following command to print to a file (I omitted the file details): cat( paste( paste(orientation, start, end, names,"\n"), paste(start, end, "exon\n"), sep="")) where "orientation" and "names" are character vectors and "start" and "end" are integer vectors. The problem is that R coerce the integer vectors to characters. In general, that works fine, but when one of the integer is 100000 (or has more 0's) then R prints it as 1e+05. This behavior causes a lot of trouble for the program reading R's output. This problem occur with paste, cat, and print (i.e. paste(100000)="1e+05" and so on). I tried to change the "digit" option in "options()" but that did not help. Is is possible to change the behavior of the coercing or are there any work arounds? Thanks in advance, Firas.
Well, you have to convert an integer to character to see it: `as is' is in your case 64 0's and 1's. I very much suspect that you have a double and not an integer:> 100000[1] 1e+05> as.integer(100000)[1] 100000 so that is one answer: actually use an `integer vector' as you claim. A second answer is in ?options, see `scipen'. A third answer is to use sprintf() or formatC() to handle the conversion yourself. On Thu, 14 Apr 2005, Firas Swidan wrote:> Hi, > I am using the following command to print to a file (I omitted the file > details): > > cat( paste( paste(orientation, start, end, names,"\n"), paste(start, end, > "exon\n"), sep="")) > > where "orientation" and "names" are character vectors and "start" and > "end" are integer vectors. > > The problem is that R coerce the integer vectors to characters. In > general, that works fine, but when one of the integer is 100000 (or has > more 0's) then R prints it as 1e+05. This behavior causes a lot of > trouble for the program reading R's output. > This problem occur with paste, cat, > and print (i.e. paste(100000)="1e+05" and so on). > > I tried to change the "digit" option in "options()" but that did not help. > Is is possible to change the behavior of the coercing or are there any > work arounds?-- 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
On Thu, Apr 14, 2005 at 02:32:33PM +0300, Firas Swidan wrote:> I am using the following command to print to a file (I omitted the file > details): > > cat( paste( paste(orientation, start, end, names,"\n"), paste(start, end, > "exon\n"), sep="")) > > where "orientation" and "names" are character vectors and "start" and > "end" are integer vectors.For printing formatted output of this kind, you're generally much better off using sprintf, as in cat(sprintf("%2s %8d %8d %s\n", orientation, as.integer(start), as.integer(end), names)); or, if length(names) > 1, you might consider sprintf("%2s %8d %8d %s\n", orientation, as.integer(start), as.integer(end), paste(names, collapse = ", ")); etc. This assumes that start and end are numeric vectors of length 1, which seems sensible to me based on the context I can conclude from the variable names, and I think that sprintf in R-devel, and R 2.1.0 in the near future will cycle over longer vectors too.> The problem is that R coerce the integer vectors to characters. In > general, that works fine, but when one of the integer is 100000 (or has > more 0's) then R prints it as 1e+05. This behavior causes a lot of > trouble for the program reading R's output. > This problem occur with paste, cat, > and print (i.e. paste(100000)="1e+05" and so on).Are you certain that start and end are integer vectors? If in doubt, check typeof(start) -- the fact that the values are integer does not necessarily mean that the type is integer. Best regards, Jan -- +- Jan T. Kim -------------------------------------------------------+ | *NEW* email: jtk at cmp.uea.ac.uk | | *NEW* WWW: http://www.cmp.uea.ac.uk/people/jtk | *-----=< hierarchical systems are for files, not for humans >=-----*