Godmar Back
2009-Jul-07 18:56 UTC
[R] how to count number of elements in a vector that are not NA ?
Hi, is there a simpler way to count the number of elements in a vector that are not NA than this: countN <- function (v) { return (Reduce(function (x, y) x + y, ifelse(is.na(v), 0, 1))) } ? - Godmar
Jorge Ivan Velez
2009-Jul-07 19:02 UTC
[R] how to count number of elements in a vector that are not NA ?
Dear Godmar, Yes. One way would be sum( !is.na( yourvector ) ) HTH, Jorge On Tue, Jul 7, 2009 at 2:56 PM, Godmar Back <godmar@gmail.com> wrote:> Hi, > > is there a simpler way to count the number of elements in a vector > that are not NA than this: > > countN <- function (v) { > return (Reduce(function (x, y) x + y, ifelse(is.na(v), 0, 1))) > } > > ? > > - Godmar > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Zhiliang Ma
2009-Jul-07 19:05 UTC
[R] how to count number of elements in a vector that are not NA ?
how about sum(!is.na(x)) ? On Tue, Jul 7, 2009 at 2:56 PM, Godmar Back<godmar at gmail.com> wrote:> Hi, > > is there a simpler way to count the number of elements in a vector > that are not NA than this: > > countN <- function (v) { > ? ?return (Reduce(function (x, y) x + y, ifelse(is.na(v), 0, 1))) > } > > ? > > ?- Godmar > > ______________________________________________ > 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. >
Henrique Dallazuanna
2009-Jul-07 19:06 UTC
[R] how to count number of elements in a vector that are not NA ?
another option should be: length(na.omit(v)) On Tue, Jul 7, 2009 at 3:56 PM, Godmar Back <godmar@gmail.com> wrote:> Hi, > > is there a simpler way to count the number of elements in a vector > that are not NA than this: > > countN <- function (v) { > return (Reduce(function (x, y) x + y, ifelse(is.na(v), 0, 1))) > } > > ? > > - Godmar > > ______________________________________________ > 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 [[alternative HTML version deleted]]
David Huffer
2009-Jul-07 19:10 UTC
[R] how to count number of elements in a vector that are not NA ?
How about countN <- function ( v ) { sum ( !is.na ( v ) ) - sum ( is.na ( v ) ) } -- David ? ----------------------------------------------------- David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huffer at csosa.gov ----------------------------------------------------- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Godmar Back Sent: Tuesday, July 07, 2009 2:57 PM To: R-help at r-project.org Subject: [R] how to count number of elements in a vector that are not NA ? Hi, is there a simpler way to count the number of elements in a vector that are not NA than this: countN <- function (v) { return (Reduce(function (x, y) x + y, ifelse(is.na(v), 0, 1))) } ? - Godmar ______________________________________________ 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.
David Huffer
2009-Jul-07 19:25 UTC
[R] how to count number of elements in a vector that are not NA ?
On Tuesday, July 07, 2009 3:20 PM, Godmar Back wrote: > ...That would be wrong, wouldn't it, if the > other replies are correct.... Yes. It was wrong. This isn't: countN <- function ( v ) { length ( v ) - sum ( is.na ( v ) ) } But there are really tons of ways to do it. Even your way is not wrong... -- David ? ----------------------------------------------------- David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huffer at csosa.gov
Giovanni Petris
2009-Jul-07 21:30 UTC
[R] how to count number of elements in a vector that are not NA ?
sum(!is.na(x))> Date: Tue, 07 Jul 2009 14:56:54 -0400 > From: Godmar Back <godmar at gmail.com> > Sender: r-help-bounces at r-project.org > Precedence: list > DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; > DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; > > Hi, > > is there a simpler way to count the number of elements in a vector > that are not NA than this: > > countN <- function (v) { > return (Reduce(function (x, y) x + y, ifelse(is.na(v), 0, 1))) > } > > ? > > - Godmar > > ______________________________________________ > 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. > >