[R] extract fixed width fields from a string

Sam Steingold sds at gnu.org
Sun Jan 22 21:34:12 CET 2012


> * Petr Savicky <fnivpxl at pf.pnf.pm> [2012-01-20 21:59:51 +0100]:
>
> Try the following.
>
>   x <-
> tolower("ThusThisLongWordWithLettersAndDigitsFrom0to9isAnIntegerBase36")
>   x <- strsplit(x, "")[[1]]
>   digits <- 0:35
>   names(digits) <- c(0:9, letters)
>   y <- digits[x]
>  
>   # solution using gmp package
>   library(gmp)
>   b <- as.bigz(36)
>   sum(y * b^(length(y):1 - 1))
>  
>   [1]
> "70455190722800243410669999246294410591724807773749367607882253153084991978813070206061584038994

thanks, here is what I wrote:

## convert a string to an integer in the given base
digits <- 0:63
names(digits) <- c(0:9, letters, toupper(letters), "-_")
string2int <- function (str, base=10) {
  d <- digits[strsplit(str,"")[[1]]]
  sum(d * base^(length(d):1 - 1))
}

and it appears to work.
however, I want to be able to apply it to all elements of a vector.
I can use apply:

> unlist(lapply(c("100","12","213"),string2int))
[1] 100  12 213

but not directly:

> string2int(c("100","12","213"))
[1] 100

thanks a lot for your help!

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 11.10 (oneiric) X 11.0.11004000
http://honestreporting.com http://thereligionofpeace.com http://camera.org
http://www.memritv.org http://openvotingconsortium.org
A man paints with his brains and not with his hands.



More information about the R-help mailing list