[R] extract fixed width fields from a string

Petr Savicky savicky at cs.cas.cz
Fri Jan 20 21:59:51 CET 2012


On Fri, Jan 20, 2012 at 03:14:21PM -0500, Sam Steingold wrote:
> On Fri, Jan 20, 2012 at 14:05, Sarah Goslee <sarah.goslee at gmail.com> wrote:
> >> then I need to convert each 6/8 character string into an integer base 36
> >> or 64 (depending on the field) - how?
> >
> > base 36?
> 
> 10 decimal digits + 26 english characters = 36.
> ThusThisLongWordWithLettersAndDigitsFrom0to9isAnIntegerBase36
> (case insensitive).
> So, how do I convert the above long word to a bignum?

Hi.

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
 
  # solution using Rmpfr package
  library(Rmpfr)
  b <- mpfr(36, precBits=500)
  sum(y * b^(length(y):1 - 1))
 
  [1] 70455190722800243410669999246294410591724807773749367607882253153084991978813070206061584038994

>actually, my numbers will fit into int64, no bignum support is necessary.

The default R numeric data type is double precision,
which represents integers up to 53 bits, so the
largest exactly representable integer is 2^53.
The integer type is 32 bits.

Hope this helps.

Petr Savicky.



More information about the R-help mailing list