如何在R中将整数转换为二进制向量?
例如:
例如:
number <- 11
[1] 1 0 1 1
如果我需要将整个数字向量(最小值=0,最大值=300)转换为二进制矩阵,使用R代码或一些现有包中的函数,最快的转换方法是什么?
跟随兔子:base::intToBits
number <- 11
[1] 1 0 1 1
如果我需要将整个数字向量(最小值=0,最大值=300)转换为二进制矩阵,使用R代码或一些现有包中的函数,最快的转换方法是什么?
跟随兔子:base::intToBits
有一个 intToBits
函数,可以将任何整数转换为长度为 32 的向量,因此您可以执行以下操作:
decimals <- c(3,5,11,4)
m <- sapply(decimals,function(x){ as.integer(intToBits(x))})
m
> m
[,1] [,2] [,3] [,4]
[1,] 1 1 1 0
[2,] 1 0 1 0
[3,] 0 1 0 1
[4,] 0 0 1 0
[5,] 0 0 0 0
[6,] 0 0 0 0
[7,] 0 0 0 0
[8,] 0 0 0 0
[9,] 0 0 0 0
[10,] 0 0 0 0
[11,] 0 0 0 0
[12,] 0 0 0 0
[13,] 0 0 0 0
[14,] 0 0 0 0
[15,] 0 0 0 0
[16,] 0 0 0 0
[17,] 0 0 0 0
[18,] 0 0 0 0
[19,] 0 0 0 0
[20,] 0 0 0 0
[21,] 0 0 0 0
[22,] 0 0 0 0
[23,] 0 0 0 0
[24,] 0 0 0 0
[25,] 0 0 0 0
[26,] 0 0 0 0
[27,] 0 0 0 0
[28,] 0 0 0 0
[29,] 0 0 0 0
[30,] 0 0 0 0
[31,] 0 0 0 0
[32,] 0 0 0 0
这篇SO帖子建议使用intToBits
函数进行转换。我定义了number2binary
函数,其中包括一个参数noBits
来控制返回的位数。默认情况下返回32位。
number2binary = function(number, noBits) {
binary_vector = rev(as.numeric(intToBits(number)))
if(missing(noBits)) {
return(binary_vector)
} else {
binary_vector[-(1:(length(binary_vector) - noBits))]
}
}
还有一些示例:
> number2binary(11)
[1] 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 1 1
> number2binary(11, 4)
[1] 1 0 1 1
length(number2binary(0,32))== 31
...? - Fabian Werner尝试使用CRAN软件包"binaryLogic"
library(binaryLogic)
as.binary(11)
[1] 1 0 1 1
as.binary(11, littleEndian=TRUE)
[1] 1 1 0 1
as.binary(42, n=16)
[1] 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0
as.binary(0:2, n=2)
[[1]]
[1] 0 0
[[2]]
[1] 0 1
[[3]]
[1] 1 0
as.binary(0xFF)
[1] 1 1 1 1 1 1 1 1
还有其他选项可用:移位、旋转、格雷码等。
intToBit
的函数来实现此功能:intToBitVect <- function(x){
tmp <- rev(as.integer(intToBits(x)))
id <- seq_len(match(1,tmp,length(tmp))-1)
tmp[-id]
}
match
检查第一个1出现的位置。如果找不到1,则要求match
返回tmp
向量的长度。seq_len
从1到第一个出现1
的位置之前创建一个序列。tmp
向量中的位置。> intToBitVect(11)
[1] 1 0 1 1
> intToBitVect(0)
[1] 0
binary <- function(x) {
i <- 0
string <- numeric(32)
while(x > 0) {
string[32 - i] <- x %% 2
x <- x %/% 2
i <- i + 1
}
first <- match(1, string)
string[first:32]
}
And another:
toBits <- function (x, nBits = 8){
tail(rev(as.numeric(intToBits(x))),nBits)
}
intToBin <- function(x){
if (x == 1)
1
else if (x == 0)
NULL
else {
mod <- x %% 2
c(intToBin((x-mod) %/% 2), mod)
}
}
[1] "1" "0" "1" "0"
如果您需要字符串而非向量
> paste0(intToBin(10), collapse = "")
[1] "1010"
dectobin <- function(y) {
# find the binary sequence corresponding to the decimal number 'y'
stopifnot(length(y) == 1, mode(y) == 'numeric')
q1 <- (y / 2) %/% 1
r <- y - q1 * 2
res = c(r)
while (q1 >= 1) {
q2 <- (q1 / 2) %/% 1
r <- q1 - q2 * 2
q1 <- q2
res = c(r, res)
}
return(res)
}
int_to_binary <- function(num, pow) {
if(2^(pow + 1) - 1 < num) stop("Use a higher values of 'pow'")
num %/% (2^(pow:0)) %% 2
}
这里是一个Rcpp的实现。
在Rstudio中,将以下代码保存在文件中,例如binary.cpp
,然后输入Source。
//************************
// binary.cpp
#include <RcppArmadillo.h>
//' @Title get the binary representation of a positive integer
// [[Rcpp::depends(RcppArmadillo)]]
//[[Rcpp::export]]
void intToBinary(int n, arma::vec& a ) {
for(int i=0;n>0;i++){
a[i]=n%2;
n=n/2;
}
}
//************************
> a <- rep(0,10) # needed to store the binary representation
> myInt <- 5 # to be converted to binary form
> intToBinary(myInt, a)
> rev(a) # print in the usual form
0000...00001011
。 - digEmAllsapply(decimals,function(x)as.integer(paste(rev( as.integer(intToBits(x))),collapse="")))
- Onyambumatrix(as.integer(intToBits(decimals)), nrow = 32)
可能会更快,因为它是矢量化的。 - jeanlain