[R] convert to binary to decimal

Bert Gunter gunter.berton at gene.com
Thu Feb 15 17:53:17 CET 2007


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
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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 stat.math.ethz.ch 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