我创建了R代码,该代码以十六进制形式产生结果。
dput(hexResults)
c("34", "c3", "3b", "7f", "14", "fd", "53", "dc", "ea", "25",
"e0", "1a", "02", "e1", "67", "27", "34", "c3", "3b", "7f", "14",
"fd", "53", "dc", "ea", "25", "e0", "1a", "02", "e1", "67", "27"
。
之后,我尝试使用包 BMS hex2bin(hexResults)
dput(hex2bin(hexResults))
c(0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1,
0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1,
1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0,
0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1,
0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1,
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1,
0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,
0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,
1, 1, 1)
将结果转换为Binary。
如果仔细看,您会发现第一个十六进制数字(34)转换为二进制,然后添加了四个零0000
,然后转换了第二个十六进制数字(c3),并再次添加了四个零,依此类推。我找不到原因,除了可能是某种填充。
现在,我想要得到的是:
我正在考虑通过遍历数据来实现这一点,这是我在将选择性数据从矢量复制到数据帧时遇到的困难。使用其他命令(例如粘贴)有什么建议吗?
答案 0 :(得分:1)
没有必要循环任何东西。您只需将向量转换成矩阵并将其另存为df。
vec <- hex2bin(hexResults)
vec <- c(vec, 0, 0, 0, 0) #add last binary that is missing
df <- as.data.frame(matrix(vec, ncol = 12, byrow = TRUE)) # get into df format
df <- df[, -c(9:12)] #remove binary zeros
df[1:2, ] #check result
V1 V2 V3 V4 V5 V6 V7 V8
1 0 0 1 1 0 1 0 0
2 1 1 0 0 0 0 1 1