[R] Character (1a, 1b) to numeric
Abby Spurdle
@purd|e@@ @end|ng |rom gm@||@com
Sun Jul 12 01:06:19 CEST 2020
On Sat, Jul 11, 2020 at 8:04 AM Fox, John <jfox using mcmaster.ca> wrote:
> We've had several solutions, and I was curious about their relative efficiency. Here's a test
Am I the only person on this mailing list who learnt to program with ASCII...?
In theory, the most ***efficient*** solution, is to get the
ASCII/UTF8/etc values.
Then use a simple (math) formula.
No matching, no searching, required ...
Here's one possibility:
xc <- c ("1", "1a", "1b", "1c", "2", "2a", "2b", "2c")
I <- (nchar (xc) == 2)
xn <- as.integer (substring (xc, 1, 1) )
xn [I] <- xn [I] + (utf8ToInt (paste (substring (xc [I], 2, 2),
collapse="") ) - 96) / 4
xn
Unfortunately, this makes R look bad.
The corresponding C implementation is simpler and presumably the
performance winner.
More information about the R-help
mailing list