[R] Newbie needs to count elements in a row
John Kane
jrkrideau at yahoo.ca
Tue Dec 29 21:52:35 CET 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)
==============================================================
-- On Tue, 12/29/09, Verena Weber <VerenaWeber at gmx.de> wrote:
> From: Verena Weber <VerenaWeber at gmx.de>
> Subject: [R] Newbie needs to count elements in a row
> To: r-help at 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 at 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.
>
More information about the R-help
mailing list