How do I transform a stock ticker to a unique integer in R?
This is useful, for example, when matching two data bases based on stock ticker symbols. The code should work for ordinary equity tickers (capital letters, 1-4 characters in length).
hash.ticker <- Vectorize( function(s, M=length(letters), MAXCHARS=4) {
v <- utf8ToInt(s) - utf8ToInt("@")
if (length(v)<MAXCHARS) v <- c(v,rep(0,MAXCHARS-length(v)))
stopifnot( all( (v >= 0) & (v <= M) ) ) ## need 0 to account for empty (0)
sum( v * (M)^(0:(length(v)-1)) )
})
print( hash.ticker( c("A", "Z", "AA", "ZZ", "ZZZZ") ) )
A Z AA ZZ ZZZZ
1 26 27 702 475254
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.