integer.base.b {dga} | R Documentation |
Takes a decimal number and converts it to base b.
integer.base.b(x, b = 2)
x |
A number. |
b |
The desired base. |
This was harvested from the internet here: https://stat.ethz.ch/pipermail/r-help/2003-September/038978.html. Posted by Spencer Graves.
A number in base b.
Spencer Graves
https://stat.ethz.ch/pipermail/r-help/2003-September/038978.html
## 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 }