

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
10 messages in org.r-project.r-help[R] convert to binary to decimal| From | Sent On | Attachments |
|---|---|---|
| Martin Feldkircher | Feb 15, 2007 7:37 am | |
| Marc Schwartz | Feb 15, 2007 8:09 am | |
| Roland Rau | Feb 15, 2007 8:21 am | |
| Bert Gunter | Feb 15, 2007 8:52 am | |
| Roland Rau | Feb 15, 2007 9:13 am | |
| Marc Schwartz | Feb 15, 2007 9:21 am | |
| Petr Pikal | Feb 15, 2007 11:19 pm | |
| Roland Rau | Feb 16, 2007 7:52 am | |
| Jim Regetz | Feb 16, 2007 5:44 pm | |
| Henrik Bengtsson | Feb 17, 2007 5:14 pm |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | [R] convert to binary to decimal | Actions... |
|---|---|---|
| From: | Bert Gunter (gunt...@gene.com) | |
| Date: | Feb 15, 2007 8:52:54 am | |
| List: | org.r-project.r-help | |
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-he...@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.







