Skip last NA's?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Skip last NA's?

Robert Lundqvist-2
I wonder if anyone could help me find an expression for skipping the last
missing values in a vector? The kind of material I have is something like

x<-c(23,12,NA,23,24,21,NA,NA,NA)

I would like to skip the last NA's, but not the ones in between other
vallues. Any hints? (Why not do this by simply take x[1:6]? I have several
vectors a couple of thousand observations long with varying numbers of
NA's in the end. I'd prefer not to search through all of these one at a
time.)

Robert

______________________________________________
[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
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Skip last NA's?

Patrick Burns
A fairly standard trick for such situations is to use

    rle(is.na(x))

In your case you want to see if the last value is TRUE.

Patrick Burns
[hidden email]
+44 (0)20 8525 0696
http://www.burns-stat.com
(home of S Poetry and "A Guide for the Unwilling S User")

Robert Lundqvist wrote:

>I wonder if anyone could help me find an expression for skipping the last
>missing values in a vector? The kind of material I have is something like
>
>x<-c(23,12,NA,23,24,21,NA,NA,NA)
>
>I would like to skip the last NA's, but not the ones in between other
>vallues. Any hints? (Why not do this by simply take x[1:6]? I have several
>vectors a couple of thousand observations long with varying numbers of
>NA's in the end. I'd prefer not to search through all of these one at a
>time.)
>
>Robert
>
>______________________________________________
>[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
>
>
>
>  
>

______________________________________________
[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
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Skip last NA's?

PIKAL Petr
In reply to this post by Robert Lundqvist-2
Hi
probably not the best solution but

if(tail(is.na(x), n=1)) x[1 : (length(x) -
(tail(rle(is.na(x))$lengths, n=1)))] else x

shall do what you want.

HTH
Petr


On 2 Mar 2006 at 9:29, Robert Lundqvist wrote:

Date sent:       Thu, 2 Mar 2006 09:29:49 +0100 (MET)
From:           Robert Lundqvist <[hidden email]>
To:             [hidden email]
Subject:         [R] Skip last NA's?
Send reply to:   [hidden email]
        <mailto:[hidden email]?subject=unsubscribe>
        <mailto:[hidden email]?subject=subscribe>

> I wonder if anyone could help me find an expression for skipping the
> last missing values in a vector? The kind of material I have is
> something like
>
> x<-c(23,12,NA,23,24,21,NA,NA,NA)
>
> I would like to skip the last NA's, but not the ones in between other
> vallues. Any hints? (Why not do this by simply take x[1:6]? I have
> several vectors a couple of thousand observations long with varying
> numbers of NA's in the end. I'd prefer not to search through all of
> these one at a time.)
>
> Robert
>
> ______________________________________________
> [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

Petr Pikal
[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
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Handling outliers?

Robert Lundqvist-2
In reply to this post by Patrick Burns
I am sitting with this fairly big material (20 variables, max length of
vectors about 3200 observations and a substantial amount of missing
values). In some cases there are also outliers. Some are obvious, others
are not that clear.

So far, I have replaced some of the outliers with NA's. However, I would
like to have a good working procedure where outliers where not excluded
permanently but rather temporarily. Some way of "marking"  observations
and still keep them seems both preferable and possible.

Any suggestions for a good working practice for cases like this? How do
*you* work? Is there any "standard" package to use?

Robert

______________________________________________
[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
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Handling outliers?

Prof Brian Ripley
It is not clear what sort of analysis you are doing, and for example
robust/resistant regression is a way of identifying and downweighting
outliers in a regression analysis.

Also, multivariate outliers are a very different concept from univariate
ones, and the difference may or may not matter depending on the analysis.

On Thu, 2 Mar 2006, Robert Lundqvist wrote:

> I am sitting with this fairly big material (20 variables, max length of
> vectors about 3200 observations and a substantial amount of missing
> values). In some cases there are also outliers. Some are obvious, others
> are not that clear.
>
> So far, I have replaced some of the outliers with NA's. However, I would
> like to have a good working procedure where outliers where not excluded
> permanently but rather temporarily. Some way of "marking"  observations
> and still keep them seems both preferable and possible.

Depends what `keep' means, but in one sense that is what
na.action = na.exclude does.

> Any suggestions for a good working practice for cases like this? How do
> *you* work? Is there any "standard" package to use?
>
> Robert

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[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
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Skip last NA's?

Gabor Grothendieck
In reply to this post by Robert Lundqvist-2
Do you mean you wish to create a vector without the trailing NAs
but with the others?  If so, try:

library(zoo)
head(x, length(na.locf(x, rev = TRUE)))

or

library(zoo)
head(x, length(na.locf(rev(x))))


On 3/2/06, Robert Lundqvist <[hidden email]> wrote:

> I wonder if anyone could help me find an expression for skipping the last
> missing values in a vector? The kind of material I have is something like
>
> x<-c(23,12,NA,23,24,21,NA,NA,NA)
>
> I would like to skip the last NA's, but not the ones in between other
> vallues. Any hints? (Why not do this by simply take x[1:6]? I have several
> vectors a couple of thousand observations long with varying numbers of
> NA's in the end. I'd prefer not to search through all of these one at a
> time.)
>
> Robert
>
> ______________________________________________
> [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
>

______________________________________________
[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
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Skip last NA's?

J. R. M. Hosking
In reply to this post by Robert Lundqvist-2
Robert Lundqvist wrote:

> I wonder if anyone could help me find an expression for skipping the last
> missing values in a vector? The kind of material I have is something like
>
> x<-c(23,12,NA,23,24,21,NA,NA,NA)
>
> I would like to skip the last NA's, but not the ones in between other
> vallues. Any hints? (Why not do this by simply take x[1:6]? I have several
> vectors a couple of thousand observations long with varying numbers of
> NA's in the end. I'd prefer not to search through all of these one at a
> time.)
>
> Robert
>
> ______________________________________________
> [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
>

x[1:max(which(!is.na(x)))]

or, to allow for cases in which x may consist entirely of NAs or have
length 0,

x[0:max(0,which(!is.na(x)))]

______________________________________________
[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
Loading...