[R] Inserting missing seq number
Jeff Reichman
re|chm@nj @end|ng |rom @bcg|ob@|@net
Wed Mar 30 13:21:12 CEST 2022
Petr & Eric
Thank you for the two options. Figured there was an easy straight forward
way.
Jeff
-----Original Message-----
From: PIKAL Petr <petr.pikal using precheza.cz>
Sent: Wednesday, March 30, 2022 12:59 AM
To: reichmanj using sbcglobal.net
Cc: r-help using r-project.org
Subject: RE: [R] Inserting missing seq number
Hallo Jeff
Similar result you will get by na.locf function from zoo package
df <- data.frame(seq = c(1,2,4,5,7), count = c(4,7,3,5,2))
vec <-data.frame(seq =1:7)
df <- merge(vec, df, all=T)
library(zoo)
df$count <- na.locf(df$count)
Cheers
Petr
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Eric Berger
> Sent: Wednesday, March 30, 2022 5:36 AM
> To: reichmanj using sbcglobal.net
> Cc: r-help using r-project.org
> Subject: Re: [R] Inserting missing seq number
>
> Hi Jeff,
> tidyr::fill() will do what you want
>
> aDf <- data.frame(seq=1:7)
> bDf <- left_join(aDf,df,by="seq")
> tidyr::fill(bDf,count)
>
> HTH,
> Eric
>
>
> On Wed, Mar 30, 2022 at 5:47 AM 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