[R] inadequacy in as.integer....
Jorgen Harmse
JH@rm@e @end|ng |rom roku@com
Tue Sep 13 16:54:57 CEST 2022
Akshay Kulkarni makes an interesting point, and R supports a further step. If you really want something that supports integer computation but retains the string information then you can shoehorn the latter into an attribute.
x <- as.integer(s) # or some computation that handles octal & hexadecimal
attr(x,�string�) <- s
Now you can compute 2L*x or whatever, but the string travels with x, so it�s easier to recover than if you put it in a separate variable. You�ll have to check the rules for when the result of a computation acquires the attributes of an input.
Regards,
Jorgen.
------------------------------
Message: 9
Date: Tue, 13 Sep 2022 07:35:02 +0000
From: akshay kulkarni <akshay_e4 using hotmail.com>
To: "avi.e.gross using gmail.com" <avi.e.gross using gmail.com>, "'R help Mailing
list'" <r-help using r-project.org>
Subject: Re: [R] inadequacy in as.integer....
Message-ID:
<PU4P216MB15686276926CB98C83D894EEC8479 using PU4P216MB1568.KORP216.PROD.OUTLOOK.COM>
Content-Type: text/plain; charset="utf-8"
Dear Avi,
Thanks for the comprehensive reply...
Yours sincerley,
AKSHAY M KULKARNI
________________________________
From: R-help <r-help-bounces using r-project.org> on behalf of avi.e.gross using gmail.com <avi.e.gross using gmail.com>
Sent: Monday, September 12, 2022 1:12 AM
To: 'R help Mailing list' <r-help using r-project.org>
Subject: Re: [R] inadequacy in as.integer....
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.
Avi
-----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
analysis:
> as.integer("09098")
Any idea on how to retain the "0"?
Yours sincerely,
AKSHAY M KULKARNI
[[alternative HTML version deleted]]
______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
------------------------------
Subject: Digest Footer
_______________________________________________
R-help using r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
------------------------------
End of R-help Digest, Vol 235, Issue 13
***************************************
[[alternative HTML version deleted]]
More information about the R-help
mailing list