[R] How to sum only a few elements in a line

David Winsemius dwinsemius at comcast.net
Wed Nov 25 21:48:46 CET 2009


On Nov 25, 2009, at 3:27 PM, Marcio Resende wrote:

>
> Hello,
>
> I have a matrix with the numbers 0,1 and 9
> I would like to write a function that could sum each line skiping  
> everytime
> a number 9 appears
> for example
> [0 1 0 1 1 9 1]

sum(x[x != 9])

> the sum would be 4.
> However I cannot replace 9 by 0 otherwise after the sum is done I  
> wouldn´t
> be able to distiguish which ones were real zeros and which ones were  
> nines
> replaced by zero just to sum.
> Thank you very much
>
> -- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list