Dear members, I came across this queer thing during my analysis:> as.integer("09098")9098 Any idea on how to retain the "0"? Yours sincerely, AKSHAY M KULKARNI [[alternative HTML version deleted]]
On 9/11/22 12:22, akshay kulkarni wrote:> Dear members, > I came across this queer thing during my analysis: >> as.integer("09098") > 9098 > > Any idea on how to retain the "0"?Don't use as.integer(), which has performed precisely the service that it advertises? I certainly wouldn't call this an 'inadequacy'. And please stop posting in html. ---JRG> Yours sincerely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
Keep as character. Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of akshay kulkarni Sent: Sunday, September 11, 2022 12:22 PM To: R help Mailing list <r-help at r-project.org> Subject: [R] inadequacy in as.integer.... [External Email] Dear members, I came across this queer thing during my analysis:> as.integer("09098")9098 Any idea on how to retain the "0"? Yours sincerely, AKSHAY M KULKARNI [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu%7C70a6ae074d1e42c17e9108da9411d934%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637985101625267716%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2FV4NoU8j%2F9KIYX5ExaY93fwQf%2FPqx1kisbt87Mj9%2F7Q%3D&reserved=0 PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C70a6ae074d1e42c17e9108da9411d934%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637985101625267716%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=BA0o8oGcR%2FFBX9Vth5DuitlZnTGG7rNDmFga3ixZ9j4%3D&reserved=0 and provide commented, minimal, self-contained, reproducible code.
You seem to be confusing **what** is printed with *how* it is printed.> print(9) ## a numeric (not an integer, actually. That would be 9L)[1] 9 ## default print format> print(formatC(9, width =2, flag = "0")) ## format specification[1] "09"> print(formatC(9, width =2, flag = "0"), quote = FALSE) # don't show quotes[1] 09 See ?formatC and ?print.default for details -- Bert On Sun, Sep 11, 2022 at 9:22 AM akshay kulkarni <akshay_e4 at hotmail.com> wrote:> > Dear members, > I came across this queer thing during my analysis: > > as.integer("09098") > 9098 > > Any idea on how to retain the "0"? > > Yours sincerely, > AKSHAY M KULKARNI > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.
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 at r-project.org> On Behalf Of akshay kulkarni Sent: Sunday, September 11, 2022 12:22 PM To: R help Mailing list <r-help at r-project.org> Subject: [R] inadequacy in as.integer.... Dear members, I came across this queer thing during my analysis:> as.integer("09098")9098 Any idea on how to retain the "0"? Yours sincerely, AKSHAY M KULKARNI [[alternative HTML version deleted]] ______________________________________________ R-help at 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.