Re: [R] Newbie needs to count elements in a row
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 3 for row y? Could you help me? I found some functions for columns but not for rows... Thank you very much! __ R-help@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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ R-help@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.
Re: [R] Newbie needs to count elements in a row
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 8:50 AM To: r-help@r-project.org Subject: [R] Newbie needs to count elements in a row 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 3 for row y? Could you help me? I found some functions for columns but not for rows... Thank you very much! __ R-help@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. === 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 locations. Confidentiality Note: This message is intended for use\...{{dropped:13}} __ R-help@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.
Re: [R] Newbie needs to count elements in a row
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) == -- On Tue, 12/29/09, Verena Weber verenawe...@gmx.de wrote: From: Verena Weber verenawe...@gmx.de Subject: [R] Newbie needs to count elements in a row To: r-help@r-project.org Received: Tuesday, December 29, 2009, 8:49 AM 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 3 for row y? Could you help me? I found some functions for columns but not for rows... [[elided Yahoo spam]] __ R-help@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. __ R-help@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.