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?