获取表以返回R中的空行

时间:2016-11-25 09:42:00

标签: r

如果我有

> pred <- c(1,0,1,0,1,0,1,0,1,0)
> actual <-c(1,1,1,1,1,0,0,0,0,0)
> table(Predictions = pred, TrueLabels = actual)
           TrueLabels
Predictions 0 1
          0 3 2
          1 2 3
> 

然后返回两行,一行用于预测假,一行用于预测真。当预测是一个还是另一个时,有没有办法让表返回两行(其中一行为空)?

> pred <- c(0,0,0,0,0,0,0,0,0,0)
> actual <-c(1,1,1,1,1,0,0,0,0,0)
> table(Predictions = pred, TrueLabels = actual)
           TrueLabels
Predictions 0 1
          0 5 5
> 

我希望它返回

           TrueLabels
Predictions 0 1
          0 5 5
          1 0 0
> 

相反,当我查看我的TN / FP等计算特异性和灵敏度时,我不必检查第二行是否存在。

1 个答案:

答案 0 :(得分:1)

关键是将其转换为factor并指定levels,以便不会删除特定组合

table(Predictions = factor(pred, levels = 0:1), TrueLabels = factor(actual, levels = 0:1))
#        TrueLabels
#Predictions 0 1
#          0 5 5
#          1 0 0