Re: [R] Filling in a series

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 21 Oct 2006 - 13:32:10 GMT

The zoo package has na.locf (last occurrernce carried foward) for this purpose:

> x

 [1] 1 NA NA NA NA 2 NA NA NA NA 3 NA NA
> library(zoo)
> na.locf(x)
 [1] 1 1 1 1 1 2 2 2 2 2 3 3 3

On 10/21/06, Dennis Fisher <fisher@plessthan.com> wrote:
> Colleagues
>
> After reading in some clinical data, I discovered that the subject ID
> column contains entries only for the first record for each
> individual; subsequent rows are recorded as NA. For example:
> > 1
> > NA
> > NA
> > NA
> > NA
> > 2
> > NA
> > NA
> > NA
> > NA
> > 3
> > NA
> > NA
> > ...
>
> I can think of various approaches to replace the NA values with
> appropriate entries. I could loop through each row - if the value is
> NA, I replace it with the entry from the row above. Or, I could find
> the positions of the non-NA values with match, then replace groups of
> entries (e.g., positions 2-5) with appropriate entries, again with a
> loop.
>
> But, I expect that R allows some more clever approach to the
> problem. Any thoughts?
>
> Dennis
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-415-564-2220
> www.PLessThan.com
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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. Received on Sun Oct 22 04:19:17 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 21 Oct 2006 - 18:30:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.