Note that there are some explanatory texts on larger screens.

plurals
  1. POI need the output of table() to be square (padded with zeros if necessary)
    primarykey
    data
    text
    <p>I'm comparing some classifiers. My procedure is to compute the confusion matrix with the table command, and then calculate the false positive and true positive rates from the table. The routine I wrote requires that the table be square. There should be an easy way to do it.</p> <p>My setup:</p> <blockquote> <p>cm &lt;- table(classifiers$teacher[which(classifiers$problem == 'problem27')],<br> classifiers$srAve[which(classifiers$problem == 'problem27')]) cm</p> </blockquote> <pre><code> 1 2 3 0 23 0 0 1 2 4 0 2 2 10 0 3 0 1 0 4 0 0 1 &gt; missingNames &lt;- as.numeric( rownames(cm)[ !(rownames(cm) %in% as.numeric(colnames(cm))) ] ) &gt; missingNames [1] 0 4 </code></pre> <p>And then the C like function I wrote to fix it: </p> <pre><code>padTable &lt;- function( missingNames, cm ) { rowLength &lt;- dim(cm)[1] for (i in missingNames) { zeroes &lt;- rep(0,rowLength) cNames &lt;- colnames(cm) after &lt;- which ( (i &lt; as.numeric(cNames)) )[1] before &lt;- which ( (i &gt; as.numeric(cNames)) )[1] if ( is.na(before) ) { #The very begining cm &lt;- cbind(zeroes,cm) colnames(cm) &lt;- c(i,cNames) } else { if (is.na(after)) { #The very end cm &lt;- cbind(cm,zeroes) colnames(cm) &lt;- c(cNames,i) } else { #somewhere in the middle print('ERROR CANNOT INSERT INTO MIDDLE YET.') cm = NULL } } } return(cm) } </code></pre> <p>So, there has to be some dreadfully simple way to make this work. Anytime I find myself writing C code in R, I know that I'm doing it wrong. </p> <p>Thanks for any help.</p> <p>EDIT: Sample data as requested: </p> <pre><code>&gt; classifiers$teacher[which(classifiers$problem == 'problem27')] [1] 0 0 1 2 2 2 0 0 0 0 0 2 0 0 2 0 4 3 0 0 2 2 0 2 0 0 2 2 1 0 0 2 1 0 1 2 0 0 [39] 0 1 0 0 1 &gt; classifiers$srAve[which(classifiers$problem == 'problem27')] [1] 1 1 2 2 2 2 1 1 1 1 1 1 1 1 1 1 3 2 1 1 2 2 1 2 1 1 2 2 1 1 1 2 2 1 2 2 1 1 [39] 1 2 1 1 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload