r - Map fixed length string to an integer -


suppose map combination of string of length k integers (if count 26 latters uppers , lowers have (26+26) ^k combination. there fast way in r map, given string of length k unique integer?

for example: 1. k=1 , c("a","d","z"), result c(1,4,26). 2. k=2 , c("aa","da","za"), result c(53,157,1379).

conv <- function(strings, base = letters) {   s <- strsplit(strings, "")    vapply(s, function(x, base) {     x <- match(x, base)     as.integer(t(x) %*% (length(base)^(rev(seq_along(x)) - 1l)))   }, 1l, base=base) }  #test strtoi("5a3", base=16) #[1] 1443 conv("5a3", base=c(1:9, letters[1:7])) #[1] 1443  mystring <- c("a", "d", "z", "aa","da","za") conv(mystring, base = c(letters, letters)) #[1]    1    4   26   53  209 1379 

other symbols can included in base parameter.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -