[R] binary numbers

Robin Hankin rksh at soc.soton.ac.uk
Thu Apr 15 12:27:39 CEST 2004


Hi Sam.  Try:

as.binary <- function(n,base=2 , r=FALSE)
{
   out <- NULL
   while(n > 0) {
     if(r) {
       out <- c(out , n%%base)
     } else {
       out <- c(n%%base , out)
     }   
     n <- n %/% base
   }
   return(out)
}

HTH

robin


>Hi,
>
>Is there a function in R that lets one represent an integer in 
>binary format for a given number of bits? So an example would be....
>
>>  binary.function(num=5, num.of.bits=8)
>>  "00000101"
>
>Or, is this something I have to write myself?
>
>Any help would be appreciated.
>
>Cheers,
>
>Sam.
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


-- 
Robin Hankin
Uncertainty Analyst
Southampton Oceanography Centre
SO14 3ZH
tel +44(0)23-8059-7743
initialDOTsurname at soc.soton.ac.uk (edit in obvious way; spam precaution)




More information about the R-help mailing list