Re: [R] Conversion to Binary (base2)

From: Erich Neuwirth <erich.neuwirth_at_univie.ac.at>
Date: Fri, 26 Sep 2008 00:55:36 +0200

Since I have to teach number base conversion within 2 weeks, I could not resist:

numberInBase <- function(number,base){
  numberInBaseRecur<-function(number,base){     lastDigit<-function(number,base) number %% base     if (number == 0) result <- c(0)
    else result <- c(numberInBaseRecur(number %/% base,base),

                     lastDigit(number,base))
    result
  }
  result <- numberInBaseRecur(number,base)   while (result[1]== 0 && length(result)>1) result <- result[-1]   result
}

makeDigitSeq <- function(digiseq){
  digits <- c(as.character(0:9),LETTERS)   paste(sapply(digiseq,function(x)digits[x+1]),collapse="") }

makeDigitSeq(numberInBase(21,2))
probably does what you want.

This works up to base 36.

Jason Thibodeau wrote:
> Hello,
>
> Is there a simple way to take an input, and convert the decimal integers to
> binary? In this case, I have a CSV file, and I need to convert the first
> column of every line to binary.
>
> Thanks.
>
>
>
> ------------------------------------------------------------------------
>
>
> No virus found in this incoming message.
> Checked by AVG - http://www.avg.com
> Version: 8.0.169 / Virus Database: 270.7.2/1689 - Release Date: 9/24/2008 6:51 PM
>

-- 
Erich Neuwirth, University of Vienna
Faculty of Computer Science
Computer Supported Didactics Working Group
Visit our SunSITE at http://sunsite.univie.ac.at
Phone: +43-1-4277-39464 Fax: +43-1-4277-39459

______________________________________________
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.
Received on Thu 25 Sep 2008 - 23:58:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 26 Sep 2008 - 08:30:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive