-1

I have been using the following code to convert decimals into binaries, however I cannot seem to convert a decimal greater than 1069 into binary.

run_id = 1:1600
run_ids = as.data.frame(run_id)
run_ids$bin = 0

for (i in 1:length(x)){
  run_ids[i,2] = as.numeric(paste(rev(as.numeric(intToBits(as.numeric(run_ids[i,1])))), collapse=""))
}

Unfortunately the range of numbers that I want to convert goes up to 1,600. I have tried to use as.double, as.numeric to fix the issue however these do not work.

What am I missing?

Thanks in advance for any help you can give me.

1 Answers1

0

Your code works after a minor correction.

The desired ouput can be obtained by replacing for(i in 1:length(x)){ with for(i in run_id){:

run_id <- 1:1600
run_ids <- as.data.frame(run_id)
run_ids$bin <- 0
for (i in run_id){
  run_ids[i,2] <- as.numeric(paste(rev(as.numeric(intToBits(as.numeric(run_ids[i,1])))), collapse=""))
}
> tail(run_ids)
#     run_id         bin
#1595   1595 11000111011
#1596   1596 11000111100
#1597   1597 11000111101
#1598   1598 11000111110
#1599   1599 11000111111
#1600   1600 11001000000
RHertel
  • 23,412
  • 5
  • 38
  • 64
  • @user5375228 I don't see the point. You say you are not able to convert decimal greater than 1069, but your code just didn't run? –  Sep 25 '15 at 08:48
  • The code worked however only up to 1069. I made a couple of modifications to it to post in the forum. However my error was as RHertel pointed out with the for loop length. Works perfectly now thank you. – user5375228 Sep 25 '15 at 23:03