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. -- Jason Thibodeau [[alternative HTML version deleted]]
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. > >Not really (unless I missed it), sprintf will convert to hex but not binary. But you might roll your own as in > binary <- function(x) if (all(x<2)) x else paste(binary(x%/%2), x%%2, sep="") > binary(1:20) [1] "00001" "00010" "00011" "00100" "00101" "00110" "00111" "01000" "01001" [10] "01010" "01011" "01100" "01101" "01110" "01111" "10000" "10001" "10010" [19] "10011" "10100" (You can fairly easily do a variant that gets rid of the leading zeros) -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On 9/25/2008 3:33 PM, 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.Yes, the intToBits function does what you want. It works with raw vector output and integer vector input, so you need a few type conversions, but essentially this is simple: > x <- 123 > paste(rev(as.integer(intToBits(as.integer(x)))), collapse="") [1] "00000000000000000000000001111011" Duncan Murdoch
See also sfsmisc:as.intBase . On Thu, 25 Sep 2008, Duncan Murdoch wrote:> On 9/25/2008 3:33 PM, 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. > > Yes, the intToBits function does what you want. It works with raw vector > output and integer vector input, so you need a few type conversions, but > essentially this is simple: > >> x <- 123 >> paste(rev(as.integer(intToBits(as.integer(x)))), collapse="") > [1] "00000000000000000000000001111011" > > Duncan Murdoch > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
This was what I was looking for to solve the truncate to 17 digits. Thanks a lot. Now my output looks like this: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 ,0.0998004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 ,0.1996008 On Thu, Sep 25, 2008 at 4:28 PM, Christos Hatzis < christos.hatzis@nuverabio.com> wrote:> paste(rev(as.integer(intToBits(as.integer(x))[1:17])), collapse="") > > -Christos > > > -----Original Message----- > > From: r-help-bounces@r-project.org > > [mailto:r-help-bounces@r-project.org] On Behalf Of Jason Thibodeau > > Sent: Thursday, September 25, 2008 4:11 PM > > To: Duncan Murdoch > > Cc: r-help@r-project.org > > Subject: Re: [R] Conversion to Binary (base2) > > > > This seems to work well. After playing with it for a while, > > however, I can't seem to find a way to fix the number of > > binary digits to say, 17. Am I just missing something, or am > > I getting lost in the type conversion? > > > > The help page for intToBits said parameter n, and I tried > > that to no avail. > > > > On Thu, Sep 25, 2008 at 3:56 PM, Duncan Murdoch > > <murdoch@stats.uwo.ca>wrote: > > > > > On 9/25/2008 3:33 PM, 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. > > >> > > > > > > Yes, the intToBits function does what you want. It works with raw > > > vector output and integer vector input, so you need a few type > > > conversions, but essentially this is simple: > > > > > > > x <- 123 > > > > paste(rev(as.integer(intToBits(as.integer(x)))), collapse="") > > > [1] "00000000000000000000000001111011" > > > > > > Duncan Murdoch > > > > > > > > > > > > -- > > Jason Thibodeau > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@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. > > > > > > >-- Jason Thibodeau [[alternative HTML version deleted]]
It occurs to me that Christos' method could be made more flexible by using rle(). That is, before collapsing the digits, you have something like >foo [1] 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 1 0 Then rle(foo) will show you where the boring lead-zeros end, and you can use that value to set the truncation ---- rather than a fixed [1:17] as below. Carl quote: This was what I was looking for to solve the truncate to 17 digits. Thanks a lot. Now my output looks like this: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ,0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 ,0.0998004 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 ,0.1996008 On Thu, Sep 25, 2008 at 4:28 PM, Christos Hatzis < christos.hatzis_at_nuverabio.com> wrote: > paste(rev(as.integer(intToBits(as.integer(x))[1:17])), collapse="") endquote
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