[R] extract fixed width fields from a string
R. Michael Weylandt
michael.weylandt at gmail.com
Sun Jan 22 21:47:57 CET 2012
To Jorge: non-necessarily base 10.
To Sam: your problem is the sum() function which collapses vectors: if
this is really performance critical to your overall task, I'd look at
doing it with Rcpp+inline. Otherwise, it might be possible to do this
using matrix operations (i.e., split your strings characterwise,
assemble into a matrix, then multiply the matrix by c(b^2, b^1, b^0)
or somesuch and finally use rowSums/colSums) if you really want to
avoid apply()
Michael
On Sun, Jan 22, 2012 at 3:40 PM, Jorge I Velez <jorgeivanvelez at gmail.com> wrote:
> 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
>
> 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.
>>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
More information about the R-help
mailing list