在R中的某些条件下向表中添加行

时间:2019-07-31 10:10:01

标签: r matrix confusion-matrix

我有以下两个向量:

> pred
 [1] "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
[19] "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"

> act
 [1] "0" "1" "0" "1" "0" "1" "0" "0" "1" "0" "1" "1" "1" "1" "1" "1" "1" "1"
[19] "1" "0" "1" "0" "0" "1" "1" "0" "1" "0" "0" "1" "1"

我正在尝试通过以下方法将这两个混淆:

> conf = table(pred, act)
> conf 
    act
pred  0  1
   1 12 19

很明显,由于pred向量的每个向量均为1,因此该混淆矩阵只有一行;第一个输入是错误的分类数量,第二个输入是正确的分类数量。

我在几种情况下都使用这些混淆矩阵,通常它们是2 x 2,第一行对应正确和不正确的分类为0。但是,在这些情况下,混淆矩阵是1 x 2 ,因此我想在第一行中将矩阵2 x 2设为0。像这样:

> matrix(c(rep(0, 2), conf[1, 1], conf[1, 2]), ncol = 2, byrow = TRUE)
     [,1] [,2]
[1,]    0    0
[2,]   12   19

我想做的是,如果此混淆矩阵的尺寸为1 x 2,则将其更改为2 x 2矩阵,否则将单独放置。我尝试过:

> conf = ifelse(nrow(conf) == 1, 
                matrix(c(rep(0, 2), conf[1, 1], conf[1, 2]), 
                       ncol = 2, byrow = TRUE), 
                conf)

但是,当我尝试此操作时,会得到以下结果:

> conf
[1] 0

我该如何解决?

1 个答案:

答案 0 :(得分:2)

您可以将string processname = ""; //the process you want to capture Process[] processlist = Process.GetProcesses(); var processTitle = processlist.Where(c => c.ProcessName == processname).ToList(); c.ProcessName == processname转换为pred,将act转换为两者的factors

levels