如何从概率矩阵创建1 0矩阵

时间:2020-09-02 22:17:14

标签: r

我有一个如下所示的概率矩阵:

ID  V1     V2         V3        V4
1  0.15     0.1        0.5     0.25
2.  0       0.1        0.3.     0.6
3.  0.2.    0.25.        0.2.     0.35

我想将其转换为1、0矩阵,将最高概率分配为1,其余概率分配为0:

ID  V1     V2         V3        V4
1   0       0          1         0
2.  0       0.         0.        1
3.  0.      0          0         1

我该如何编写函数来完成任务?

2 个答案:

答案 0 :(得分:4)

如果是按行,那么我们用max得到pmax的值,并与数据集进行比较

df1[-1] <- +(df1[-1] == do.call(pmax, df1[-1]))
df1
#  ID V1 V2 V3 V4
#1  1  0  0  1  0
#2  2  0  0  0  1
#3  3  0  0  0  1

或与apply

df1[-1] <- t(apply(df1[-1], 1, function(x) +(x == max(x))))

数据

df1 <- structure(list(ID = c(1, 2, 3), V1 = c("0.15", "0", "0.2."), 
    V2 = c("0.1", "0.1", "0.25."), V3 = c("0.5", "0.3.", "0.2."
    ), V4 = c(0.25, 0.6, 0.35)), class = "data.frame", 
    row.names = c(NA, 
-3L))

答案 1 :(得分:1)

我们可以使用max.col返回每一行的最大值索引。

#Create a copy of df
df2 <- df
#turn all values to 0
df2[-1] <- 0
#Get the max column number in each row and turn those values to 1
df2[cbind(1:nrow(df), max.col(df[-1]) + 1)] <- 1
df2

#  ID V1 V2 V3 V4
#1  1  0  0  1  0
#2  2  0  0  0  1
#3  3  0  0  0  1

数据

df <- structure(list(ID = 1:3, V1 = c(0.15, 0, 0.2), V2 = c(0.1, 0.1, 
0.25), V3 = c(0.5, 0.3, 0.2), V4 = c(0.25, 0.6, 0.35)), 
class = "data.frame", row.names = c(NA, -3L))