从 table 中读取数字并将结果写到 R 中的新 table 中?

Read numbers from a table and write the results down into a new table in R?

我将使用此脚本将许多十进制数转换为二进制数:(例如,此处: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)

参考:Converting decimal to binary in R?

它有效,但我需要 R 从 table 中读取 x 值(此处:Book1.csv 和 C 列)。

所以,我添加了这个脚本:

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

在这一点之后我应该写什么,以使 R 能够完成这些工作:

1- 从 Book1.csv 文件(C 列)读取 x 值。

2- 使用上述脚本将所有 x 值转换为二进制。

3- 最后,将每个 z 值保存在 test.csv 中。

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

编辑:

非常感谢你抽出时间@Daniel。

这是我在 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")

这是 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")

看来我使用你的脚本是正确的,但我不知道为什么结果显示这个错误:

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

这些是警告列表:

> 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

我更改了 R 的版本,但错误仍然存​​在。 您使用的是哪个版本的 R?

你必须在下面的例子中定义开始和结束行,以及列索引,否则,如果你想要一个列的所有值,只需指定列索引:

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

这是一个可重现的数据示例,其中假定 x 的值来自数据框的列:

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")

所有转换后的二进制值都连接起来并保存在 newvar 中。然后将原始值和转换后的二进制数保存到磁盘 write.csv.

注意:如果整数过大,as.raw似乎不​​起作用。但是,您可以简单地省略它并使用 intToBits.

将值直接转换为位