[R] extract fixed width fields from a string

Sam Steingold sds at gnu.org
Sun Jan 22 21:45:23 CET 2012


> * Jorge I Velez <wbetrvinairyrm at tznvy.pbz> [2012-01-22 15:40:09 -0500]:
>
> What is wrong with as.numeric()?
>
>> as.numeric(c("100","12","213"))
> [1] 100  12 213
>> sum(as.numeric(c("100","12","213")))
> [1] 325

as.numeric handles only decimals; I need other bases too (36 & 64)

> HTH,
> Jorge
>
>
> On Sun, Jan 22, 2012 at 3:34 PM, Sam Steingold <> wrote:
>
>> > * Petr Savicky <> [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.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> 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.
>>

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 11.10 (oneiric) X 11.0.11004000
http://palestinefacts.org http://honestreporting.com http://mideasttruth.com
http://pmw.org.il http://www.PetitionOnline.com/tap12009/ http://jihadwatch.org
Bill Gates is not god and Microsoft is not heaven.



More information about the R-help mailing list