how to count number of elements in a vector that are not NA ?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

how to count number of elements in a vector that are not NA ?

Godmar Back
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to count number of elements in a vector that are not NA ?

Jorge I Velez
Dear Godmar,
Yes. One way would be

sum( !is.na( yourvector ) )

HTH,

Jorge



On Tue, Jul 7, 2009 at 2:56 PM, Godmar Back <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to count number of elements in a vector that are not NA ?

Zhiliang Ma
In reply to this post by Godmar Back
how about sum(!is.na(x)) ?

On Tue, Jul 7, 2009 at 2:56 PM, Godmar Back<[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to count number of elements in a vector that are not NA ?

Henrique Dallazuanna
In reply to this post by Godmar Back
another option should be:

length(na.omit(v))

On Tue, Jul 7, 2009 at 3:56 PM, Godmar Back <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to count number of elements in a vector that are not NA ?

HufferD
In reply to this post by Godmar Back
How about

countN <- function ( v ) {
  sum ( !is.na ( v ) ) - sum ( is.na ( v ) )
}

--
 David
 
 -----------------------------------------------------
 David Huffer, Ph.D.               Senior Statistician
 CSOSA/Washington, DC           [hidden email]
 -----------------------------------------------------

-----Original Message-----
From: [hidden email] [mailto:[hidden email]] On Behalf Of Godmar Back
Sent: Tuesday, July 07, 2009 2:57 PM
To: [hidden email]
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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to count number of elements in a vector that are not NA ?

HufferD
In reply to this post by Godmar Back
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           [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to count number of elements in a vector that are not NA ?

Godmar Back
In reply to this post by Henrique Dallazuanna
Thank you for the many replies! This is really a very friendly and
helpful community!

On Tue, Jul 7, 2009 at 3:06 PM, Henrique Dallazuanna<[hidden email]> wrote:
> another option should be:
>
> length(na.omit(v))
>

I think the above is what I was looking for since, presumably, it uses
the very same test as other methods that exploit na.*, such as
cor.test.

... so I tried it out and, of course, R's typing trips me over:

> length(na.omit(q[[51]]))
[1] 1

Why?  I think I know: q[[51]] is a data frame with 1 component, and
'length' applied to a data frame counts its components.  Not a
problem, I can do:

> length(na.omit(q[[51]][,1]))
[1] 110

or

> length(na.omit(q[[51]])[,1])
[1] 110

for this data frame of 1 components.

Do other functions act like length()?  Let's look at sum():

> sum(na.omit(q[[51]]))
[1] 132
> sum(na.omit(q[[51]][,1]))
[1] 132
> sum(na.omit(q[[51]])[,1])
[1] 132

Nope.

Easy to get wrong.  I buy the last two, but the first? I assume sum()
applied to a data frame reduces all its elements across all
components(?).  Letting the call "sum(na.omit(q[[51]]))" fail would
have been a sensible design decision too, IMO. One needs to be really
careful in R.

Thank you again,

 - Godmar

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: how to count number of elements in a vector that are not NA ?

Giovanni Petris
In reply to this post by Godmar Back

sum(!is.na(x))

> Date: Tue, 07 Jul 2009 14:56:54 -0400
> From: Godmar Back <[hidden email]>
> Sender: [hidden email]
> 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
>
> ______________________________________________
> [hidden email] 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.
>
>

______________________________________________
[hidden email] 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.