If the result is limited to just the counts of 1,2 and 3, you can do the following:
> habs <- matrix(sample(1:3, 50, TRUE, prob=c(.6,.2,.2)),5,10) > habs [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 1 3 2 1 1 3 1 1 1 2 [2,] 1 2 3 1 3 1 1 1 1 1 [3,] 1 2 3 2 3 1 2 1 1 3 [4,] 3 1 3 1 2 2 1 1 1 2 [5,] 1 1 2 2 1 1 3 1 3 2 > result <- apply(habs, 1, function(x){ + c('1' = sum(x == 1), '2' = sum(x==2), '3' = sum(x == 3)) + }) > cbind(habs, t(result)) 1 2 3 [1,] 1 3 2 1 1 3 1 1 1 2 6 2 2 [2,] 1 2 3 1 3 1 1 1 1 1 7 1 2 [3,] 1 2 3 2 3 1 2 1 1 3 4 3 3 [4,] 3 1 3 1 2 2 1 1 1 2 5 3 2 [5,] 1 1 2 2 1 1 3 1 3 2 5 3 2 On Wed, Jun 8, 2011 at 2:23 AM, the_big_kowalski <bkowal...@csumb.edu> wrote: > Hi, > > I have a matrix > [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] > [1,] 1 3 1 1 3 1 1 2 3 2 > [2,] 1 3 1 2 1 1 1 2 2 1 > [3,] 1 2 2 3 2 1 1 1 2 1 > [4,] 3 2 1 1 1 3 1 2 1 2 > [5,] 1 1 2 2 1 1 3 1 1 2 > and want to determine how many times 1s, 2s, and 3s occur per each row. > I tried using 'table', but end up with frequencies for the whole matrix. > Thanks in advance for your help > > -- > View this message in context: > http://r.789695.n4.nabble.com/determine-frequencies-in-a-matrix-by-row-tp3581733p3581733.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > -- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.