[R] Character (1a, 1b) to numeric
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Sun Jul 12 02:04:39 CEST 2020
I'll admit that I cut my teeth on ASCII, but I worried about your
reliance on that ancient typographic ordering. I wrote a little
function:
al2num_sub<-function(x) {
xspl<-unlist(strsplit(x,""))
if(length(xspl) > 1)
xspl<-paste(xspl[1],which(letters==xspl[2]),sep=".")
return(xspl)
}
unlist(sapply(xc,al2num_sub(xc)))
that does the trick with ASCII, but there was a nagging worry that it
wouldn't work for any ordering apart from the Roman alphabet.
Unfortunately I couldn't find any way to substitute something for
"letters" that would allow me to plug in a more general solution like:
alpha.set<-c("letters","greek",...)
Maybe someone else can crack that one.
Jim
On Sun, Jul 12, 2020 at 9:07 AM Abby Spurdle <spurdle.a using gmail.com> wrote:
>
> 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.
>
> ______________________________________________
> 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.
More information about the R-help
mailing list