将具有多列的输出列表转换为 R 中的 table

Transforming an output list with multiple columns into a table in R

我有一个来自 R 的输出 table,类型为 "list":

> print(confMat)
Cross-Validated (10 fold, repeated 3 times) Confusion Matrix 

(entries are percentual average cell counts across resamples)

          Reference
Prediction Feeding Foraging Standing
  Feeding       44        0        2
  Foraging       0       32        0
  Standing       0        0       22

 Accuracy (average) : 0.98

> typeof(confMat)
[1] "list"

我想将它转换成 table 和 headers 这样的:

             Feeding Foraging Standing
  Feeding       44        0        2
  Foraging       0       32        0
  Standing       0        0       22

这样我就可以提取 table 中的值,如下所示:

x<-confMat[1:1]
print(x)
44

我试过使用:

confMat <- data.frame(matrix(unlist(confMat), nrow=length(confMat), byrow=T))

但是,我没有得到想要的输出:

> dput(confMat)
structure(list(X1 = structure(c(3L, 1L, 2L, 4L), .Label = c("0", 
"2", "44", "overall"), class = "factor"), X2 = structure(c(1L, 
3L, 1L, 2L), .Label = c("0", "30", "32"), class = "factor"), 
    X3 = structure(c(1L, 1L, 2L, 3L), .Label = c("0", "22", "Cross-Validated (10 fold, repeated 3 times) Confusion Matrix"
    ), class = "factor")), class = "data.frame", row.names = c(NA, 
-4L))

如果这个问题太基础了,我很抱歉。我是 R 的新手,所以我希望有人能帮助我!欢迎任何意见。

解压如下图:

 library(caret)
train_set<-createDataPartition(iris$Species,p=0.8,list=FALSE)
valid_set<-iris[-train_set,]
train_set<-iris[train_set,]
ctrl<-trainControl(method="cv",number=5)
set.seed(233)
mk<-train(Species~.,data=train_set,method="knn",trControl = ctrl,metric="Accuracy")

目标

  confusionMatrix(mk)["table"][[1]]
            Reference
Prediction       setosa versicolor  virginica
  setosa     33.3333333  0.0000000  0.0000000
  versicolor  0.0000000 32.5000000  2.5000000
  virginica   0.0000000  0.8333333 30.8333333