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

Gabor Grothendieck ggrothendieck at gmail.com
Sun Nov 11 18:47:27 CET 2007


On Nov 11, 2007 11:23 AM, David Winsemius <dwinsemius at comcast.net> wrote:
> It worked. But this student of R is left wondering why or how. In
> particular the construction <foo>[.][.] was puzzling. I doubt that
> matters greatly, but I am using R in both a Mac (OSX 10.2 v 2.0.1) and
> WinXP (v 2.4.1.)
>
> I have broken it into what I think are its pieces:
>  > table(my.mat)[-1]
> my.mat
> 1 2
> 9 4
> # a count of the non-zero elements
>  > table(my.mat)[-1][my.mat]
> my.mat
> 1 1 1 1 1 1 1 1 1 2 2 2 2
> 9 9 9 9 9 9 9 9 9 4 4 4 4
>
> I am having some trouble figuring out how the second index, the matrix,
> my.mat, is being handled by the table object ... if that is what is
> happening after the semantic parsing.

foo[x][y] is not a special construct.  Its just [y] applied to the
result of foo[x].

table(my.mat)[-1][my.mat]

is the same as

(table(my.mat)[-1])[my.mat]

or:

tmp <-  table(my.mat)[-1]
tmp[my.mat]

The [-1] removes the first component, i.e. the one that corresponds to 0,
so that only the ones corresponding to 1 and 2 are left.  Then we index
that one by my.mat.  Those components of my.mat that are 0 do not
produce anything in the output leaving the indexing by 1 and 2 as the only
ones left.

The solution was specific to the fact that 0, 1 and 2 were used but a more
general solution that allows gaps between the numbers would be the following
(where we have used mm in place of my.mat) :

mm[] <- table(mm)[factor(mm)] * c(mm > 0)

>
> I have searched for material on "subscripting" and found a rather small
> amount, but after looking the  help file, I see that I should have been
> looking for "indexing" and "Extract". It appears that "[.]" is an
> operation that is represented internally by Extract(.) and Extract both
> extracts and replaces.
>
> Most of the discussions of "recursive indexing" I found were directed at
> the "[[" operation, but is this construct also recursive indexing?
>
> --
> Sincerely;
> David Winsemius
>
>
>  Gabor Grothendieck wrote:
> > 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.
> >>
> >>
> >
> > ______________________________________________
> > 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.
> >
> >
>
> ______________________________________________
> 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