Reputation: 3661
I have a vector of actual values (factor variables) a a a b b c c b c b c c ...
I have a vector of predict values a b b a ...
I want to create a confusion matrix t = table (actual, predict)
If I print t
, it will looks like:
a b c
a 4 3 1
b 1 5 2
c 3 1 8
However, I want to print it as
b c a
b 8 2 3
c 2 3 4
a 1 2 3
(i.e, I want to change the order of row and columns, but keep it as a confusion matrix)
How could I do that in R?
Upvotes: 1
Views: 1042
Reputation: 887118
We could change/convert the columns to factor
with levels specified.
actual <- factor(actual, levels=c('b', 'c', 'a'))
predict <- factor( predict, levels = c('b', 'c', 'a'))
table(actual, predict)
# predict
#actual b c a
# b 0 1 4
# c 3 2 2
# a 2 3 3
Or we can use row/column
indexing
table(actual, predict)[c('b','c','a'), c('b', 'c', 'a')]
set.seed(24)
actual <- sample(letters[1:3], 20, replace=TRUE)
predict <- sample(letters[1:3], 20, replace=TRUE)
Upvotes: 3