matthieugomez / statar

R package for data manipulation — inspired by Stata's API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

fill_gap roll = +-1 is not working

AndreMikulec opened this issue · comments

fill_gap roll = +-1 is not working.
Or, I do not understand how the function works.

require(dplyr)
library(lubridate)
df <- data_frame(
    id    = c(1, 1, 1, 2),
    datem  = as.monthly(mdy(c("04/03/1992", "01/04/1992", "03/15/1992", "05/11/1992"))),
    value = c(4.1, 4.5, 3.3, 3.2)
)

df2 <- df
df2[2,3] <- NA_real_
df2[3,3] <- NA_real_

> plyr::arrange(df2, datem)
# A tibble: 4 x 3
     id         datem value
  <dbl> <S3: monthly> <dbl>
1     1        1992m1    NA
2     1        1992m3    NA
3     1        1992m4   4.1
4     2        1992m5   3.2

> fill_gap(plyr::arrange(df2, datem), datem, roll = -1) # 
# A tibble: 5 x 3
     id         datem value
  <dbl> <S3: monthly> <dbl>
1     1        1992m1    NA
2     1        1992m2    NA
3     1        1992m3    NA  # Why not 4.1 ** ? **
4     1        1992m4   4.1
5     2        1992m5   3.2

I expected to see

3     1        1992m3    4.1

Instead I saw

3     1        1992m3    NA

What do I need to do to make it right?

This is intended. fill_gap only modifies newly created rows. It never changes the ones that already exists.