[datatable-help] Sum first 3 non zero elements of row

JNV jose at memo2.nl
Tue May 21 11:10:30 CEST 2013


Hi there,
I've got this matrix D with, say 10 rows and 20 columns. For each row I want
to sum the first 3 non zero elements and put them in a vector z. 

So if the first row D[1,] is
0 3 5 0 8 9 3 2 4 0 

then I want z 
z<-D[1,2]+D[1,3]+D[1,5]

But if there are less than 3 non zero elements, those should be summed. If
there are no non zero elements, the result must be zero.

So if the first row D[1,] is
0 0 3 0 1 0 0 0 0 0

then I want z 
z<-D[1,3]+D[1,5]

Hope someone can help me out!



--
View this message in context: http://r.789695.n4.nabble.com/Sum-first-3-non-zero-elements-of-row-tp4667563.html
Sent from the datatable-help mailing list archive at Nabble.com.


More information about the datatable-help mailing list