1

I am going to convert many decimal numbers to binary using this script: (for example, here: 1073956868)

x <- 1073956868
y <- intToBits(x)
z <- paste(sapply(strsplit(paste(rev(y)),""),`[[`,2),collapse="")

print (z)
"01000000000000110100100000000100"

write.table(z, file = 'test.csv', row.names=FALSE,col.names=FALSE)

Ref: Converting decimal to binary in R?

It works, but I need R to read x values from a table (here: Book1.csv and column C).

So, I have added this script:

setwd("F:/test")
data1 <- read.table(file="F:/test/Book1.csv", header=T, sep=",")
data1
attach(data1) 

What should I write after this point, to enable R to do these jobs:

1- Read x values from the Book1.csv file (column C).

2- Convert all x values to binary, using the above script.

3- And finally, save each of z values in the test.csv .

========================================================================

Edit:

I would appreciate your time @Daniel.

This is the script that I am running in R-Studio:

setwd("F:/test")
data1 <- read.table(file="F:/test/Book1.csv", header=T, sep=",")
data1
attach(data1)
x <- data1[, 2]
x
newvar <- c()
for (y in x) newvar <- c(newvar, 
                         paste(sapply(strsplit(paste(rev(intToBits(as.raw(y)))), ""),
                                      `[[`, 2),
                               collapse = ""))
newvar
write.csv(newvar, file = "test.csv")

And this is the output in Console:

> data1
     row         QC    
1  34952 1073741825
2  34959 1073956868 
3  35012 1075585053   
4  35019 1075800097     
5  35063 1077151797      
6  63351 1946172419   
7  63411 1948015647     
8  65126 2000701251    
9  65186 2002544479     
10 65237 2004111223       
11 65535 2013265923      

> x
 [1] 1073741825 1073956868 1075585053 1075800097 1077151797 1946172419 1948015647 2000701251 2002544479 2004111223 2013265923

> newvar <- c()

> for (y in x) newvar <- c(newvar,
+ paste(sapply(strsplit(paste(rev(intToBits(as.raw(y)))), ""),
+ `[[`, 2),
+ collapse = ""))

There were 11 warnings (use warnings() to see them)

> newvar
 [1] "00000000000000000000000000000000" "00000000000000000000000000000000" "00000000000000000000000000000000" "00000000000000000000000000000000"
 [5] "00000000000000000000000000000000" "00000000000000000000000000000000" "00000000000000000000000000000000" "00000000000000000000000000000000"
 [9] "00000000000000000000000000000000" "00000000000000000000000000000000" "00000000000000000000000000000000"
> write.csv(newvar, file = "test.csv")

It seems I used your script correctly, but I do not know why this error is shown in the results:

There were 11 warnings (use warnings() to see them)

These are the warnings list:

> warnings()
Warning messages:
1: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw
2: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw
3: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw
4: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw
5: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw
6: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw
7: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw
8: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw
9: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw
10: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw
11: In intToBits(as.raw(y)) : out-of-range values treated as 0 in coercion to raw

I changed the R 's version, but still the error is there. What version of R you are using?

Community
  • 1
  • 1
Rebecca
  • 15
  • 4
  • I have changed my answer accordingly. Seems that integers are too large for `as.raw`, and it seems that you don't need this command anyway in your case. – Daniel Aug 03 '15 at 22:58
  • Excellent @Daniel, it works finally. Your script generates the correct values in R console without any error. **BUT** the only problem is that the final results in the excel file are different than the results in R console. Have a look into this photo, please: (http://s3.picofile.com/file/8204142092/r.jpg) . I think we need to have "" around each integer value in order to get rid of this error in excel file. What do you think? – Rebecca Aug 03 '15 at 23:47
  • Open the csv-file in a text editor: The values are already surrounded by quotes. It's just Excel that does what it shouldn't do, i.e. converting numeric strings to numbers. You can try to change the column / cell style in Excel and change it to "text", or you use LibreOffice to open the csv-file. This is no longer an R-issue, I would say. – Daniel Aug 04 '15 at 06:42

1 Answers1

0

You have to define start- and endrow, as well as column index in the below example, else, if you want all values of a column, simply specify the column index:

# specify row and column index here
x <- data1[startrow:endrow, column]

Here's a reproducible example with your data, where x is assumed to have values from a data frame's column:

x <- c(1073741825, 1073956868, 1075585053, 1075800097, 1077151797, 1946172419, 
       1948015647, 2000701251, 2002544479, 2004111223, 2013265923)
newvar <- c()
for (y in x) newvar <- c(newvar, 
                         paste(sapply(strsplit(paste(rev(intToBits(y))), ""),
                                      `[[`, 2),
                               collapse = ""))
newvar
write.csv(data.frame(int = x, bin = newvar), file = "test.csv")

all converted binary values are concatenated and saved in newvar. The original value and the converted binary number are then saved to disk with write.csv.

Note: If the integer numbers are too large, as.raw does not seems to work. However, you can simply omit it and convert values directly into bits with intToBits.

Daniel
  • 7,252
  • 6
  • 26
  • 38
  • Thanks @Daniel. But it concatenates all converted values into one long number. I want to have converted x values, separately, in a unique excel cell as shows this photo: [link]http://s6.picofile.com/file/8203916268/csv.jpg) . In other words, I want R picks an x value up from the column, then converts it to binary, and finally put the result in an excel cell. And do it several times for other x values. Thanks again. – Rebecca Aug 02 '15 at 20:19
  • Does it? I have no R at hand, but my try resulted in "separated" values in the csv-file. I will check it again. – Daniel Aug 02 '15 at 20:59
  • You probably haven't copied my code properly, it works fine for me. I added an reproducible example. – Daniel Aug 03 '15 at 12:25
  • Thank you so much @Daniel for your help. I have updated my question with the script that I am running. Do you have any idea about the error shown in the result? – Rebecca Aug 03 '15 at 21:36