Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4241688
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:20:32+00:00 2026-05-21T03:20:32+00:00

I am using the following R code to produce a confusion matrix comparing the

  • 0

I am using the following R code to produce a confusion matrix comparing the true labels of some data to the output of a neural network.

t <- table(as.factor(test.labels), as.factor(nnetpredict))

However, sometimes the neural network doesn’t predict any of a certain class, so the table isn’t square (as, for example, there are 5 levels in the test.labels factor, but only 3 levels in the nnetpredict factor). I want to make the table square by adding in any factor levels necessary, and setting their counts to zero.

How should I go about doing this?

Example:

> table(as.factor(a), as.factor(b))

    1 2 3 4 5 6 7 8 9 10
  1 1 0 0 0 0 0 0 1 0  0
  2 0 1 0 0 0 0 0 0 1  0
  3 0 0 1 0 0 0 0 0 0  1
  4 0 0 0 1 0 0 0 0 0  0
  5 0 0 0 0 1 0 0 0 0  0
  6 0 0 0 0 0 1 0 0 0  0
  7 0 0 0 0 0 0 1 0 0  0

You can see in the table above that there are 7 rows, but 10 columns, because the a factor only has 7 levels, whereas the b factor has 10 levels. What I want to do is to pad the table with zeros so that the row labels and the column labels are the same, and the matrix is square. From the example above, this would produce:

    1 2 3 4 5 6 7 8 9 10
  1  1 0 0 0 0 0 0 1 0  0
  2  0 1 0 0 0 0 0 0 1  0
  3  0 0 1 0 0 0 0 0 0  1
  4  0 0 0 1 0 0 0 0 0  0
  5  0 0 0 0 1 0 0 0 0  0
  6  0 0 0 0 0 1 0 0 0  0
  7  0 0 0 0 0 0 1 0 0  0
  8  0 0 0 0 0 0 0 0 0  0
  9  0 0 0 0 0 0 0 0 0  0
  10 0 0 0 0 0 0 0 0 0  0

The reason I need to do this is two-fold:

  • For display to users/in reports
  • So that I can use a function to calculate the Kappa statistic, which requires a table formatted like this (square, same row and col labels)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:20:33+00:00Added an answer on May 21, 2026 at 3:20 am

    EDIT – round II to address the additional details in the question. I deleted my first answer since it wasn’t relevant anymore.

    This has produced the desired output for the test cases I’ve given it, but I definitely advise testing thoroughly with your real data. The approach here is to find the full list of levels for both inputs into the table and set that full list as the levels before generating the table.

    squareTable <- function(x,y) {
        x <- factor(x)
        y <- factor(y)
    
        commonLevels <- sort(unique(c(levels(x), levels(y))))
    
        x <- factor(x, levels = commonLevels)
        y <- factor(y, levels = commonLevels)
    
        table(x,y)
    
    }
    

    Two test cases:

    > #Test case 1
    > set.seed(1)
    > x <- factor(sample(0:9, 100, TRUE))
    > y <- factor(sample(3:7, 100, TRUE))
    > 
    > table(x,y)
       y
    x   3 4 5 6 7
      0 2 1 3 1 0
      1 1 0 2 3 0
      2 1 0 3 4 3
      3 0 3 6 3 2
      4 4 4 3 2 1
      5 2 2 0 1 0
      6 1 2 3 2 3
      7 3 3 3 4 2
      8 0 4 1 2 4
      9 2 1 0 0 3
    > squareTable(x,y)
       y
    x   0 1 2 3 4 5 6 7 8 9
      0 0 0 0 2 1 3 1 0 0 0
      1 0 0 0 1 0 2 3 0 0 0
      2 0 0 0 1 0 3 4 3 0 0
      3 0 0 0 0 3 6 3 2 0 0
      4 0 0 0 4 4 3 2 1 0 0
      5 0 0 0 2 2 0 1 0 0 0
      6 0 0 0 1 2 3 2 3 0 0
      7 0 0 0 3 3 3 4 2 0 0
      8 0 0 0 0 4 1 2 4 0 0
      9 0 0 0 2 1 0 0 3 0 0
    > squareTable(y,x)
       y
    x   0 1 2 3 4 5 6 7 8 9
      0 0 0 0 0 0 0 0 0 0 0
      1 0 0 0 0 0 0 0 0 0 0
      2 0 0 0 0 0 0 0 0 0 0
      3 2 1 1 0 4 2 1 3 0 2
      4 1 0 0 3 4 2 2 3 4 1
      5 3 2 3 6 3 0 3 3 1 0
      6 1 3 4 3 2 1 2 4 2 0
      7 0 0 3 2 1 0 3 2 4 3
      8 0 0 0 0 0 0 0 0 0 0
      9 0 0 0 0 0 0 0 0 0 0
    > 
    > #Test case 2
    > set.seed(1)
    > xx <- factor(sample(0:2, 100, TRUE))
    > yy <- factor(sample(3:5, 100, TRUE))
    > 
    > table(xx,yy)
       yy
    xx   3  4  5
      0  4 14  9
      1 14 15  9
      2 11 11 13
    > squareTable(xx,yy)
       y
    x    0  1  2  3  4  5
      0  0  0  0  4 14  9
      1  0  0  0 14 15  9
      2  0  0  0 11 11 13
      3  0  0  0  0  0  0
      4  0  0  0  0  0  0
      5  0  0  0  0  0  0
    > squareTable(yy,xx)
       y
    x    0  1  2  3  4  5
      0  0  0  0  0  0  0
      1  0  0  0  0  0  0
      2  0  0  0  0  0  0
      3  4 14 11  0  0  0
      4 14 15 11  0  0  0
      5  9  9 13  0  0  0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following code within the JCProperty class to retrieve data from a
I can't understand why does the following code produce memory leaks (I am using
I've done some searching and I think the following code is guaranteed to produce
I am using the following code to produce an array: // Create new customer
Using the following code I get a nice formatted string: Request.QueryString.ToString Gives me something
I'm using the following code to query a database from my jsp, but I'd
I've been using the following code to open Office Documents, PDF, etc. on my
I am using following PHP code to connect to MS Access database: $odb_conn =
I am currently using the following code to create a web request: Dim myRequest
I'm using the following code, using the SharpZipLib library, to add files to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.