[R] Printing integers in R "as is"

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Apr 14 18:13:21 CEST 2005


On Thu, 14 Apr 2005, Firas Swidan wrote:

> Hi,
>
> thanks for the suggestions. However, for some reason the first one did not
> work. Trying
>
> cat( paste( paste(orientation, as.integer(start), as.integer(end),
> names,"\n"), paste(as.integer(start), as.integer(end),"exon\n"), sep=""))
>
> resulted in the same problem.

Works for me:

orientation <- pi
start <- 100000
end <- start+1
names <- letters[1:3]
cat( paste( paste(orientation, as.integer(start), as.integer(end),
names,"\n"), paste(as.integer(start), as.integer(end),"exon\n"), sep=""))
3.14159265358979 100000 100001 a
100000 100001 exon
  3.14159265358979 100000 100001 b
100000 100001 exon
  3.14159265358979 100000 100001 c
100000 100001 exon

whereas without the as.integer I do get 1e+05.


> Setting scipen in options did the job.
>
> Cheers,
> 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 ParksRoad,                   +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK              Fax:  +44 1865 272595
>>
>
>

-- 
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




More information about the R-help mailing list