Ruslan
Ruslan

Reputation: 951

Getting proportions of two classifiers in Matrix

I have a matrix (myData) of data such as:

      L   SCORE
[1,] "nL" 1                
[2,] "nL" 4                
[3,] "L"  4                
[4,] "L"  4                
[5,] "nL" 3                
[6,] "nL" 9   
...
...

And so on.

I would like to get a count of "L" and "nL" cases with respect to Score. The target output would be something like:

     SCORE LCount nLCount 
[1,]  1       0      1
[2,]  4       2      1   
[3,]  3       0      1              
[4,]  9       0      1
...

How can I achieve that? I tried using table(myData) but this does not produce the required results (I get a count based on Score, but no separated to "L" and "nL"). I'm feeling like I am missing something rather trivial here. How can I get the count with respect to "L" and "nL"? Currently, myData is a (rather large) matrix, and due to memory issues I'd like to avoid converting it, if possible.

Upvotes: 2

Views: 49

Answers (2)

Colonel Beauvel
Colonel Beauvel

Reputation: 31181

You can also use data.table (remember a matrix is one type so your initial matrix is just full of characters):

library(data.table)
library(reshape2)

dcast(setDT(as.data.frame(m))[,.(count=.N),.(SCORE, L)], SCORE~L, value.var='count')
#  SCORE  L nL
#1     1 NA  1
#2     3 NA  1
#3     4  2  1
#4     9 NA  1

Data

m=structure(c("nL", "nL", "L", "L", "nL", "nL", "1", "4", "4", 
"4", "3", "9"), .Dim = c(6L, 2L), .Dimnames = list(NULL, c("L", 
"SCORE")))

Upvotes: 1

Ruslan
Ruslan

Reputation: 951

Ok, answer found, and it was table() after all. Simply use:

table(myData[,1], myData[,2])

viola.

-Ruslan

Upvotes: 2

Related Questions