Converting Decimal to Binary in R
February 9, 2012 § Leave a Comment
A common task, say in conducting probability simulations, often involves enumerating every possible outcome of a set. It is often useful to use a binary representation of the size of our set to do this. A flexible way to get this representation is in matrix form, which will then allow the usual matrix operations. Here I present one such algorithm and given an implementation in R. The algorithm is iterative. It works on vectors of any length including 1 (a number). And, the algorithm returns an m x n matrix where m is the length of the longest binary digit and n is the length of the vector of numbers you give it.
To get m, the length of the longest binary digit, we use the logarithm (base 2):
For example, we know that
in binary, so
I think the easiest way to explain what we’re going to do here is just walk through some pseudo-code:
In R we have:
Let’s do a couple of examples:
Converting the number 10 to binary (1010)
Converting each of the numbers in the vector (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) to binary (1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010)
Since it performs the operations vector-wise the entire algorithm is earth-shatteringly fast! Next time I will show how this function can be used as the workhorse in an “all-subsets” regression. That is, when we want to try out every possible model specification on some data.