R - 如何转换表示bin编号的一组列中的十进制列

时间:2018-03-14 11:22:45

标签: r binary

我有这种类型的数据框: enter image description here

我想将Positions列的十进制数转换为16位二进制数: enter image description here

因此,在第二个标签中,Positions列应替换为16列。

  • “NULL”应替换为:0000000000000000
  • 5544应替换为:0001010110101000
  • 4096应替换为:0000000000001000

此刻我想出了这个:

Impl_Pos[which(Impl_Pos == "NULL")] <- 0
Impl_Pos <- c(paste(Impl_Pos))
View(Impl_Pos)

Impl_Pos_Unl=unlist(Impl_Pos, use.names=FALSE)
Impl_Pos <- sapply(Impl_Pos_Unl,function(x){as.integer(intToBits(x))})
Impl_Pos <- t(Impl_Pos[1:16,])

这导致16x4整数矩阵(此处仅显示第一组列): enter image description here

但是给这些列赋予名称似乎并不是那么简单,也不能将它们与我的其他数据集合并。 我尝试了这两组代码,但没有结果:

colnames(Impl_Pos) <- paste('pos', 1:16)
aggregate(pos 16 + pos 15 +...+ pos 2 + pos 1, Impl_Pos, sum)

library("plyr")
ddply(Impl_Pos, .(pos 16 + pos 15 +...+ pos 2 + pos 1), numcolwise(sum))
aggregate(Impl_Pos[,c(pos 16 + pos 15 +...+ pos 2 + pos 1)], by=list(Impl_Pos$Molar), "sum") 

1 个答案:

答案 0 :(得分:0)

16列中的二进制文件

如果您想将十进制值炸成16列,请尝试以下操作:

#Creating a dummy data frame similar to your example
df <- data.frame(ID = c("A","B","C"),Positions = c("NULL","5445","453"))

#Making sure that "Positions" column has characters instead of factors
df$Positions <- as.character(df$Positions)

#Impute the "NULL" values with a character "0"
df$Positions[which(df$Positions == "NULL")] <- "0"

#Change characters to integers.
df$Positions <- as.integer(df$Positions)

Impl_Pos <- sapply(df$Positions , function(x){as.integer(intToBits(x))})

#Do not transpose here. Only select the first 16 elements
Impl_Pos <- Impl_Pos[1:16,]

#Apply the "rev" (reverse) function columnwise to make 
#the order of the binary representation more intutive
Impl_Pos <- t(apply(Impl_Pos ,2,rev))

#Creating column names. I felt a numbering from 15 to 0
#might be more intuitive as they correspond to the powers of 2 at that position
pos <- rep("Pos",16)
c_names <- paste0(pos,15:0)
colnames(Impl_Pos) <- c_names

#Attach to the original df
df <- cbind(df,Impl_Pos)

我得到的输出是:

ID Positions Pos15 Pos14 Pos13 Pos12 Pos11 Pos10 Pos9 Pos8 Pos7 Pos6 Pos5 Pos4 Pos3 Pos2 Pos1 Pos0
A         0     0     0     0     0     0     0    0    0    0    0    0    0    0    0    0    0
B      5445     0     0     0     1     0     1    0    1    0    1    0    0    0    1    0    1
C       453     0     0     0     0     0     0    0    1    1    1    0    0    0    1    0    1

替代解决方案(建议)

根据您的问题,您正在尝试将二进制文件拆分为16列。如果它适合您将整个二进制文件放在一列中(对我来说很直观) - 请参阅下面的代码

#Creating a dummy data frame similar to your example
df <- data.frame(ID = c("A","B","C"),Positions = c("NULL","5445","453"))

#Making sure that "Positions" column has characters instead of factors
df$Positions <- as.character(df$Positions)

#Impute the "NULL" values with a character "0"
df$Positions[which(df$Positions == "NULL")] <- "0"

#Change characters to integers.
df$Positions <- as.integer(df$Positions)

#Vectorizing a function on the Positions column
##intToBits give a vector of binaries
##select the first 16 (as you needed only that precision)
##reverse that vector to get it in the desired format
##Use collapse = "" to merge the vector to a single string while pasting

bin_vec <-
  sapply(df$Positions, function(x) {
    temp <- intToBits(x)
    temp <- paste0(rev(temp[1:16]), collapse = "")
    return(temp)
  })

#Bind vector as column to original df
df <- cbind(df,Binary = bin_vec)

我得到的输出是:

ID Positions           Binary
A         0 0000000000000000
B      5445 0001010101000101
C       453 0000000111000101