[R] attribing frequency of "levels" to each matrix cell
David Winsemius
dwinsemius at comcast.net
Sun Nov 11 17:23:55 CET 2007
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.
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.
>
>
More information about the R-help
mailing list