integer.base.b {dga}R Documentation

Base Converter

Description

Takes a decimal number and converts it to base b.

Usage

integer.base.b(x, b = 2)

Arguments

x

A number.

b

The desired base.

Details

This was harvested from the internet here: https://stat.ethz.ch/pipermail/r-help/2003-September/038978.html. Posted by Spencer Graves.

Value

A number in base b.

Author(s)

Spencer Graves

References

https://stat.ethz.ch/pipermail/r-help/2003-September/038978.html

Examples



## The function is currently defined as
function (x, b = 2) 
{
    xi <- as.integer(x)
    if (any(is.na(xi) | ((x - xi) != 0))) 
        print(list(ERROR = "x not integer", x = x))
    N <- length(x)
    xMax <- max(x)
    ndigits <- (floor(logb(xMax, base = 2)) + 1)
    Base.b <- array(NA, dim = c(N, ndigits))
    for (i in 1:ndigits) {
        Base.b[, ndigits - i + 1] <- (x%%b)
        x <- (x%/%b)
    }
    if (N == 1) 
        Base.b[1, ]
    else Base.b
  }

[Package dga version 1.2 Index]