[R] Inserting missing seq number

Jim Lemon drj|m|emon @end|ng |rom gm@||@com
Wed Mar 30 22:55:21 CEST 2022


That is not only an elegant solution, but exactly what the OP
requested. I assume it can be generalized:

approx(x=df$seq,df$count,xout=min(df$seq):max(df$seq),method="constant",f=0)

Jim

On Thu, Mar 31, 2022 at 2:41 AM Bill Dunlap <williamwdunlap using gmail.com> wrote:
>
> stats::approx can do the job:
>
> > approx(x=df$seq, df$count, xout=1:7, method="constant", f=0)
> $x
> [1] 1 2 3 4 5 6 7
>
> $y
> [1] 4 7 7 3 5 5 2
>
> -Bill
>
> On Tue, Mar 29, 2022 at 7:47 PM Jeff Reichman <reichmanj using sbcglobal.net>
> wrote:
>
> > R-help
> >
> > Is there a R function that will insert missing sequence number(s) and then
> > fill a missing observation with the preceding value.
> >
> > For example df <- data.frame(seq = c(1,2,4,5,7), count = c(4,7,3,5,2))
> >
> >   seq count
> > 1    1        4
> > 2    2        7
> > 3    4        3
> > 4    5        5
> > 5    7        2
> >
> > What I need is
> >
> >   seq count
> > 1    1        4
> > 2    2        7
> > 3    3        7
> > 4    4        3
> > 5    5        5
> > 6    6        5
> > 7    7        2
> >
> > Jeff
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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