# fill_gap: Add rows corresponding to gaps in some variable In statar: Tools Inspired by 'Stata' to Manipulate Tabular Data

## Description

Add rows corresponding to gaps in some variable

## Usage

 ```1 2 3 4 5 6 7 8``` ```fill_gap( x, ..., full = FALSE, roll = FALSE, rollends = if (roll == "nearest") c(TRUE, TRUE) else if (roll >= 0) c(FALSE, TRUE) else c(TRUE, FALSE) ) ```

## Arguments

 `x` A data frame `...` a time variable `full` A boolean. When full = FALSE (default), the function creates rows corresponding to all missing times between the min and max of `...` within each group. When full = TRUE, the function creates rows corresponding to all missing times between the min and max of `...` in the whole dataset. `roll` When roll is a positive number, values are carried forward. roll=TRUE is equivalent to roll=+Inf. When roll is a negative number, values are rolled backwards; i.e., next observation carried backwards (NOCB). Use -Inf for unlimited roll back. When roll is "nearest", the nearest value is used. Default to FALSE (no rolling) `rollends` A logical vector length 2 (a single logical is recycled). When rolling forward (e.g. roll=TRUE) if a value is past the last observation within each group defined by the join columns, rollends[2]=TRUE will roll the last value forwards. rollends[1]=TRUE will roll the first value backwards if the value is before it. If rollends=FALSE the value of i must fall in a gap in x but not after the end or before the beginning of the data, for that group defined by all but the last join column. When roll is a finite number, that limit is also applied when rolling the end

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11``` ```library(dplyr) library(lubridate) df <- tibble( id = c(1, 1, 1, 1), datem = as.monthly(mdy(c("01/01/1992", "02/01/1992", "04/01/1992", "7/11/1992"))), value = c(4.1, 4.5, 3.3, 3.2) ) df %>% group_by(id) %>% fill_gap(datem) df %>% group_by(id) %>% fill_gap(datem, roll = 1) df %>% group_by(id) %>% fill_gap(datem, roll = "nearest") df %>% group_by(id) %>% fill_gap(datem, roll = "nearest", full = TRUE) ```

### Example output

```Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

filter, lag

The following objects are masked from 'package:base':

intersect, setdiff, setequal, union

Attaching package: 'lubridate'

The following object is masked from 'package:base':

date

id  datem
1:  1 1992m1
2:  1 1992m2
3:  1 1992m3
4:  1 1992m4
5:  1 1992m5
6:  1 1992m6
7:  1 1992m7
# A tibble: 7 x 3
# Groups:   id [1]
id datem         value
<dbl> <S3: monthly> <dbl>
1     1 1992m1          4.1
2     1 1992m2          4.5
3     1 1992m3         NA
4     1 1992m4          3.3
5     1 1992m5         NA
6     1 1992m6         NA
7     1 1992m7          3.2
id  datem
1:  1 1992m1
2:  1 1992m2
3:  1 1992m3
4:  1 1992m4
5:  1 1992m5
6:  1 1992m6
7:  1 1992m7
# A tibble: 7 x 3
# Groups:   id [1]
id datem         value
<dbl> <S3: monthly> <dbl>
1     1 1992m1          4.1
2     1 1992m2          4.5
3     1 1992m3          4.5
4     1 1992m4          3.3
5     1 1992m5          3.3
6     1 1992m6         NA
7     1 1992m7          3.2
id  datem
1:  1 1992m1
2:  1 1992m2
3:  1 1992m3
4:  1 1992m4
5:  1 1992m5
6:  1 1992m6
7:  1 1992m7
# A tibble: 7 x 3
# Groups:   id [1]
id datem         value
<dbl> <S3: monthly> <dbl>
1     1 1992m1          4.1
2     1 1992m2          4.5
3     1 1992m3          4.5
4     1 1992m4          3.3
5     1 1992m5          3.3
6     1 1992m6          3.2
7     1 1992m7          3.2
id  datem
1:  1 1992m1
2:  1 1992m2
3:  1 1992m3
4:  1 1992m4
5:  1 1992m5
6:  1 1992m6
7:  1 1992m7
# A tibble: 7 x 3
# Groups:   id [1]
id datem         value
<dbl> <S3: monthly> <dbl>
1     1 1992m1          4.1
2     1 1992m2          4.5
3     1 1992m3          4.5
4     1 1992m4          3.3
5     1 1992m5          3.3
6     1 1992m6          3.2
7     1 1992m7          3.2
```

statar documentation built on Jan. 13, 2021, 9:33 p.m.