[R] number of zeros in a matrix -row by row

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Mon Apr 6 09:21:04 CEST 2009


well, you said you tried

rowSums(M == 0)

but according to the error message it seems that you tried

rowSums[M == 0]

Try to use parenthesis '(' instead of brackets '['.


Best,
Dimitris

onyourmark wrote:
> Dear Dimitris,
> 
> Thanks very much. I tried dim(M) and get:
> [1] 5030 2142
> 
> which I thought meant that my matrix M is actually a matrix, but
> I tried  str(M) and get:
> 
>  int [1:5030, 1:2142] 2 1 2 0 1 0 2 1 2 -1 ...
> 
> When I tried 
> rowSums(M == 0)
> 
> I got:
> Error in rowSums[M == 0] : 
>   object of type 'closure' is not subsettable
> 
> 
> What am I doing wrong?
> 
> Thanks so much.
> 
> 
> 
> Dimitris Rizopoulos-4 wrote:
>> if 'M' is your matrix, then try this:
>>
>> rowSums(M == 0)
>>
>>
>> I hope it helps.
>>
>> Best,
>> Dimitris
>>
>>
>> onyourmark wrote:
>>> Hi. I have an n x m matrix M some of who's entries are zeros. I want to
>>> know
>>> how many zeros there are in each row    -perhaps stored in a 1 x n vector
>>> which lists the number of zeros for each row of M.
>>>
>>> Before I had a vector V and I was able to get the number of zeros in V by
>>> doing length(V[ V==0]) but when I try something similar for M, like M[
>>> M==0]
>>> it creates a vector not a matrix and so this does not work. Does anyone
>>> have
>>> a solution to this?
>>>
>>> Thank you.
>> -- 
>> Dimitris Rizopoulos
>> Assistant Professor
>> Department of Biostatistics
>> Erasmus University Medical Center
>>
>> Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
>> Tel: +31/(0)10/7043478
>> Fax: +31/(0)10/7043014
>>
>> ______________________________________________
>> 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.
>>
>>
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list