[R] convert to binary to decimal

Marc Schwartz marc_schwartz at comcast.net
Thu Feb 15 18:22:19 CET 2007


Clearly, my game is off at the moment...  ;-)

Thanks guys.

Marc

<Off to find another pot of coffee...>

On Thu, 2007-02-15 at 12:13 -0500, Roland Rau wrote:
> Hi Bert,
> 
> First, I was very happy with my solution, but you win (see below)!
> 
> Best,
> Roland
> 
> 
> > bert.gunter <- function(x) {
> +   sum(x * 2^(rev(seq(along=x)) - 1))
> + }
> > 
> > marc.schwartz <- function(x) {
> +   x <- as.character(as.numeric(x))
> +   b <- as.numeric(unlist(strsplit(x, "")))
> +   pow <- 2 ^ ((length(b) - 1):0)
> +   sum(pow[b == 1])
> + }
> > 
> > length( huge.list)
> [1] 20000
> > head(huge.list, n=1)
> [[1]]
>  [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
> TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE
> [21] FALSE FALSE FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE
> TRUE FALSE FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE 
> [41] FALSE FALSE FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE  TRUE
> 
> > 
> > system.time(lapply(X=huge.list, FUN=bin2dec.easy))
> [1] 2.33 0.00 2.32   NA   NA
> > system.time(lapply(X=huge.list, FUN=bin2dec.recursive ))
> [1] 14.91  0.00 14.90    NA    NA
> > system.time(lapply(X=huge.list, FUN=marc.schwartz))
> [1] 5.31 0.00 5.31   NA   NA
> > system.time(lapply(X=huge.list, FUN=bert.gunter))
> [1] 1.33 0.00 1.33   NA   NA 
> > 
> 
> 
> 
> On 2/15/07, Bert Gunter <gunter.berton at gene.com> wrote:
>         why not simply:
>         
>         sum(x * 2^(rev(seq_along(x)) - 1))   ?
>         
>         
>         Bert Gunter
>         Genentech Nonclinical Statistics
>         South San Francisco, CA 94404
>         650-467-7374
>         
>         
>         Bert Gunter
>         Nonclinical Statistics
>         7-7374
>         
>         -----Original Message-----
>         From: r-help-bounces at stat.math.ethz.ch
>         [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Roland
>         Rau
>         Sent: Thursday, February 15, 2007 8:22 AM
>         To: marc_schwartz at comcast.net
>         Cc: r-help at stat.math.ethz.ch
>         Subject: Re: [R] convert to binary to decimal
>         
>         That was a nice quick distraction. Unfortunately, I am not the
>         first to
>         answer. :-(
>         Anyway, I offer two solutions (which are different from the
>         one of Marc 
>         Schwartz); I wrote it quickly but I hope they are correct.
>         
>         Enjoy and thanks,
>         Roland
>         
>         a <- c(TRUE, FALSE, TRUE)
>         b <- c(TRUE, FALSE, TRUE, TRUE)
>         
>         bin2dec.easy <- function(binaryvector) { 
>           sum(2^(which(rev(binaryvector)==TRUE)-1))
>         }
>         
>         bin2dec.recursive <- function(binaryvector) {
>           reversed.input <- rev(binaryvector)
>           binaryhelper(reversed.input, 0, 0)
>         }
>         
>         binaryhelper <- function(binvector, currentpower,
>         currentresult) { 
>           if (length(binvector)<1) {
>             currentresult
>           } else {
>             if (binvector[1]) {
>               binaryhelper(binvector[-1], currentpower+1,
>         currentresult+2^currentpower)
>             } else {
>               binaryhelper(binvector[-1], currentpower+1,
>         currentresult) 
>             }
>           }
>         }
>         
>         
>         bin2dec.easy(a)
>         bin2dec.recursive(a)
>         bin2dec.easy(b)
>         bin2dec.recursive(b)
>         
>         
>         
>         
>         
>         On 2/15/07, Marc Schwartz <marc_schwartz at comcast.net> wrote:
>         >
>         > On Thu, 2007-02-15 at 16:38 +0100, Martin Feldkircher wrote:
>         > > Hello,
>         > > we need to convert a logical vector to a (decimal)
>         integer. Example:
>         > >
>         > > a=c(TRUE, FALSE, TRUE) (binary number 101) 
>         > >
>         > > the function we are looking for should return
>         > >
>         > > dec2bin(a)=5
>         > >
>         > > Is there a package for such a function or is it even
>         implemented in the
>         > > base package? We found the hexmode and octmode command,
>         but not a 
>         > > binmode. We know how to program it ourselves however we
>         are looking for
>         > > a computationally efficient algorithm.
>         > >
>         > > Martin and Stefan
>         >
>         > This is a modification of a function that I had posted a
>         while back, so 
>         > that it handles 'x' as a logical vector. I added the first
>         line in the
>         > function to convert the logical vector to it's numeric
>         equivalent and
>         > then coerce to character:
>         >
>         > bin2dec <- function(x) 
>         > {
>         >   x <- as.character(as.numeric(x))
>         >   b <- as.numeric(unlist(strsplit(x, "")))
>         >   pow <- 2 ^ ((length(b) - 1):0)
>         >   sum(pow[b == 1])
>         > }
>         >
>         >
>         > a <- c(TRUE, FALSE, TRUE)
>         >
>         > > bin2dec(a)
>         > [1] 5
>         >
>         > HTH,
>         >
>         > Marc Schwartz
>         >
>



More information about the R-help mailing list