Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Frequency of vectors-vectors in matrix

Tags:

r

vector

Let's say I have the following in 12x3 matrix

m<-rbind(c(0,7,0),c(1,1,5),c(6,1,1),c(1,3,-3),c(1,3,-3),c(1,1,5),
c(0,7,0),c(1,1,5),c(1,1,5),-c(0,7,0),c(1,1,5),c(1,3,-3))

and would like count the number of times each row-vector occurs. What do I do?

I have tried to use table(), but table() only counts the elements.

like image 354
AAB Avatar asked Mar 16 '23 12:03

AAB


1 Answers

You could convert each row to a concatenated string and then use table.

m <- apply(m, 1, function(x) paste(x, collapse=" "))
table(m)

m

0 -7 0  0 7 0  1 1 5 1 3 -3  6 1 1 
     1      2      5      3      1 
like image 72
cdeterman Avatar answered Apr 01 '23 06:04

cdeterman