[R] attribing frequency of "levels" to each matrix cell

Gabor Grothendieck ggrothendieck at gmail.com
Sat Nov 10 14:50:14 CET 2007


Try this:

 my.mat[my.mat > 0] <- table(my.mat)[-1][my.mat]


On Nov 10, 2007 8:19 AM, Milton Cezar Ribeiro <milton_ruser at yahoo.com.br> wrote:
> Hi R-gurus,
>
> I have a matrix which looks like
>
> 00000000000
> 01110000220
> 01110000220
> 01110000000
> 00000000000
>
> As you can see we have non-zero levels 1 and 2. I would like to fill an other matrix with the frequency of non-zero levels in each cell. The results that I need is
>
> 00000000000
> 09990000440
> 09990000440
> 09990000000
> 00000000000
>
> If I run the script below I can simulate the first matrix and count the cells for each non-zero levels. My question is how can I fill the second matrix in a easy way.
>
> my.mat<-matrix(
>  c(0,0,0,0,0,0,0,0,0,0,0,
>  0,1,1,1,0,0,0,0,2,2,0,
>  0,1,1,1,0,0,0,0,2,2,0,
>  0,1,1,1,0,0,0,0,0,0,0,
>  0,0,0,0,0,0,0,0,0,0,0),nrow=5,byrow=T)
> my.mat.freq<-data.frame(table(my.mat))
> my.mat.freq<-subset(my.mat.freq,my.mat.freq$my.mat!=0)
> my.mat.freq
>
>
> Any idea?
>
> Kind regards
>
> Miltinho
>
>
>
>  para armazenamento!
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list