@vi@e@gross m@iii@g oii gm@ii@com
2022-Jul-15 00:21 UTC
[Rd] Feature Request: Allow Underscore Separated Numbers
Devin, I cannot say anyone wants to tweak R after the fact to accept numeric items with underscores as that might impact all kinds of places. Can I suggest a workaround that allows you to enter your integer (or floating point which gets truncated) using this: underint <- function(text) as.integer(gsub("_+", "", text)) Use a call to that anywhere you want an int like: result <- underint("1_000_000") + underint("6___6__6_6") - 6000 results in: 100666 If you want to see the result with underscores, using something like scales::comma as in You can also make similar functions that use as.numeric() and as.double() but note that this allows you to enter data at somewhat greater expense and as text/strings. Obviously a similar technique can be used with regular expressions of many kinds to wipe out or replace anything, including commas with this: undernumeric <- function(text) as.numeric(gsub("[,_]+", "", text)) undernumeric("123,456.789_012") [1] 123456.8 Yes, it truncated it but I am sure any combo of underscores and commas will be removed. It also truncates the same thing with all numerals and a period. -----Original Message----- From: R-devel <r-devel-bounces at r-project.org> On Behalf Of Devin Marlin Sent: Thursday, July 14, 2022 3:54 PM To: r-devel at r-project.org Subject: [Rd] Feature Request: Allow Underscore Separated Numbers Hello, After using R for a number of years, and venturing into other languages, I've noticed the ones with the ability to enter numbers separated by underscores for readability (like 100000 as 100_000) make life a whole lot easier, especially when debugging. Is this a feature that could be implemented in R? Regards, -- *Devin Marlin* [[alternative HTML version deleted]] ______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
On 2022-07-14 8:21 p.m., avi.e.gross at gmail.com wrote:> Devin, > > I cannot say anyone wants to tweak R after the fact to accept numeric items > with underscores as that might impact all kinds of places. > > Can I suggest a workaround that allows you to enter your integer (or > floating point which gets truncated) using this: > > underint <- function(text) as.integer(gsub("_+", "", text)) > > Use a call to that anywhere you want an int like: > > result <- underint("1_000_000") + underint("6___6__6_6") - 6000 > > results in: 100666 > > If you want to see the result with underscores, using something like > scales::comma as in > > You can also make similar functions that use as.numeric() and as.double() > but note that this allows you to enter data at somewhat greater expense and > as text/strings. Obviously a similar technique can be used with regular > expressions of many kinds to wipe out or replace anything, including commas > with this: > > undernumeric <- function(text) as.numeric(gsub("[,_]+", "", text)) > > undernumeric("123,456.789_012") > [1] 123456.8 > > Yes, it truncated it but I am sure any combo of underscores and commas will > be removed. It also truncates the same thing with all numerals and a period. >It's not really 'truncated', it's just printed with limited precision. (Sorry if I'm telling you something you already know ...) options(digits = 22) undernumeric("123,456.789_012") [1] 123456.7890119999938179 (and there's floating point inaccuracy rearing its ugly head again; options(digits=16) works well for this example ...)> > > -----Original Message----- > From: R-devel <r-devel-bounces at r-project.org> On Behalf Of Devin Marlin > Sent: Thursday, July 14, 2022 3:54 PM > To: r-devel at r-project.org > Subject: [Rd] Feature Request: Allow Underscore Separated Numbers > > Hello, > > After using R for a number of years, and venturing into other languages, > I've noticed the ones with the ability to enter numbers separated by > underscores for readability (like 100000 as 100_000) make life a whole lot > easier, especially when debugging. Is this a feature that could be > implemented in R? > > Regards, > > -- > *Devin Marlin* > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- Dr. Benjamin Bolker Professor, Mathematics & Statistics and Biology, McMaster University Director, School of Computational Science and Engineering (Acting) Graduate chair, Mathematics & Statistics
Jan van der Laan
2022-Jul-15 08:27 UTC
[Rd] Feature Request: Allow Underscore Separated Numbers
Another R-solution would be: `%,%` <- function(a, b) a*1000 + b which would allow one to write large numbers as > 100%,%123 Resulting in 100123. Not sure if this really helps with readability. I actually think this could better be handled by the IDE one is working in. Most IDE's already do syntax highlighting and when a suitable font is used text as '!=' is displayed as '?' (the unequal sign). So I guess they can also apply special formatting to large numbers such as grouping the numbers, underlining groups of three, using colour, .. Jan On 15-07-2022 02:21, avi.e.gross at gmail.com wrote:> Devin, > > I cannot say anyone wants to tweak R after the fact to accept numeric items > with underscores as that might impact all kinds of places. > > Can I suggest a workaround that allows you to enter your integer (or > floating point which gets truncated) using this: > > underint <- function(text) as.integer(gsub("_+", "", text)) > > Use a call to that anywhere you want an int like: > > result <- underint("1_000_000") + underint("6___6__6_6") - 6000 > > results in: 100666 > > If you want to see the result with underscores, using something like > scales::comma as in > > You can also make similar functions that use as.numeric() and as.double() > but note that this allows you to enter data at somewhat greater expense and > as text/strings. Obviously a similar technique can be used with regular > expressions of many kinds to wipe out or replace anything, including commas > with this: > > undernumeric <- function(text) as.numeric(gsub("[,_]+", "", text)) > > undernumeric("123,456.789_012") > [1] 123456.8 > > Yes, it truncated it but I am sure any combo of underscores and commas will > be removed. It also truncates the same thing with all numerals and a period. > > > > -----Original Message----- > From: R-devel <r-devel-bounces at r-project.org> On Behalf Of Devin Marlin > Sent: Thursday, July 14, 2022 3:54 PM > To: r-devel at r-project.org > Subject: [Rd] Feature Request: Allow Underscore Separated Numbers > > Hello, > > After using R for a number of years, and venturing into other languages, > I've noticed the ones with the ability to enter numbers separated by > underscores for readability (like 100000 as 100_000) make life a whole lot > easier, especially when debugging. Is this a feature that could be > implemented in R? > > Regards, > > -- > *Devin Marlin* > > [[alternative HTML version deleted]] > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel