[R] inadequacy in as.integer....

@vi@e@gross m@iii@g oii gm@ii@com @vi@e@gross m@iii@g oii gm@ii@com
Sun Sep 11 21:42:54 CEST 2022

As others pointed out, if you call as.integer()  it converts a string, if
possible, to a particular storage method for an integer. Leading zeroes
either are ignored or in some cases may mean the number is in hexadecimal as
in "0x400" 

If you really must keep track of leading zeroes then keep the string
representation around and use that OR use a plan B. The storage
representation of an integer as 32 bits or 64 bits, or of a float/double,
does not retain any info on leading zeroes.

You can for example pass a string like "0123" or "000000123" and count the
leading zeroes in one of many ways. In the examples above, these are 1 and 6
leading zeroes respectively. There are many ways to count them from the
string version and save that number in a variable. 

Latter, if you need to include the leading zeroes then you can put them back
and combine it. As one of many ways, you can match a regular expression
against "000000123" that matches any initial run of zero or more zeroes
(saving the result) and then matches the remainder of the string, again
perhaps remembering it. This method would now store not the number of zeroes
but a string like "000000" alongside another string for "123".

If some method like that is useful, it can of course be instantiated as some
kind of R object (S3, S4, etc.) that can accept an arbitrary string version
of a number and break it into addressable parts including string and numeric
representations and when printed, recombine them and so on.

But the bottom line is that R is not planning on meeting your specific need.
There may be a package out there that has similar needs BUT that does not
mean everything else will work with such an implementation. Mostly integers
are integers with no leading zeroes.


-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of akshay kulkarni
Sent: Sunday, September 11, 2022 12:22 PM
To: R help Mailing list <r-help using r-project.org>
Subject: [R] inadequacy in as.integer....

Dear members,
                             I came across this queer thing during my
> as.integer("09098")

Any idea on how to retain the "0"?

Yours sincerely,

	[[alternative HTML version deleted]]

R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list