我创建了R代码,该代码在Hexadecimal中产生结果。
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 包将结果转换为Binary 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)
。
如果仔细看,您会注意到第一个十六进制数字(34)转换为二进制,然后0000
添加了4个零,然后它转换了第二个数字(c3),再次添加了4个零,依此类推。我找不到原因除了它可能是某种填充。
现在,我想要得到的是:
我正在考虑通过遍历数据来实现此目的,这是将选择性数据从向量复制到数据帧的困难。使用其他命令(例如粘贴)有什么建议吗?
不需要循环任何东西。您只需将向量转换成矩阵并将其另存为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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句