[R] Newbie needs to count elements in a row

Nutter, Benjamin NutterB at ccf.org
Tue Dec 29 14:54:21 CET 2009

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-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of Verena Weber
Sent: Tuesday, December 29, 2009 8:50 AM
To: r-help at r-project.org
Subject: [R] Newbie needs to count elements in a row


I have a n*m matrix and would like to count the number of elements not
equal to NA in a ROW.


x 1 2 3 NA 10
y 2 NA 8 9 NA

Which function can I use to obtain
"4" for row x and
"3" for row y?

Could you help me? I found some functions for columns but not for

Thank you very much!

R-help at r-project.org mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.


P Please consider the environment before printing this e-mail

Cleveland Clinic is ranked one of the top hospitals
in America by U.S.News & World Report (2009).  
Visit us online at http://www.clevelandclinic.org for
a complete listing of our services, staff and

Confidentiality Note:  This message is intended for use\...{{dropped:13}}

More information about the R-help mailing list