Try this:
rowSums(!is.na(x))
On Tue, Dec 29, 2009 at 11:49 AM, Verena Weber verenawe...@gmx.de wrote:
Hi,
I have a n*m matrix and would like to count the number of elements not equal
to NA in a ROW.
e.g.
x 1 2 3 NA 10
y 2 NA 8 9 NA
Which function can I use to obtain
4 for row x and
For a single row where mat is your matrix and r is the row
sum(!is.na(mat[r,]))
For every row in the matrix
rowSums(!is.na(mat))
-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Verena Weber
Sent: Tuesday, December 29, 2009
Could you just transpose the matrix?
Otherwise you can write a simple function that should work.
Try this
-(mat1 - matrix(c(1, 2, 3, NA, 10, 2, NA, 8, 9, NA),nrow=2))
gl - function(x)length(x[!is.na(x)]
apply(mat1, 1, gl)
3 matches
Mail list logo