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

问题描述 投票:-1回答:1

我有一个来自R的输出表,其类型为"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"

我想将它转换为带有标题的表:

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

这样我就可以按如下方式提取表中的值:

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的新手,所以我希望有人可以帮助我!任何输入都表示赞赏。

r list r-caret confusion-matrix
1个回答
0
投票

你可以提取它,如下所示:

 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
© www.soinside.com 2019 - 2024. All rights reserved.