Skip to content

Feature request: fill(.direction = "sandwich") #1562

Closed
@jl5000

Description

@jl5000

It would be useful if there was an option for fill to only populate missing values if they are sandwiched by identical values before and after (with an option to decide the maximum number of sequential NAs for the filling to happen) - this is a common use case. Current solutions I've found seem to require the zoo package.

It might also be worth considering some kind of tolerance for numerics, or being able to provide a callback as a function of the two sandwich values for more niche use cases.


Reprex courtesy of Mario Reyes: https://stackoverflow.com/questions/51704128/na-filling-only-if-sandwiched-by-the-same-value-using-dplyr

tibble::tibble(id= 1:12,
       var1 = letters[1:12],
       var2 = c(NA,rep("A",2),rep(NA,2),rep("A",2),rep(NA,2),rep("B",2),NA))
#> # A tibble: 12 x 3
#>       id var1  var2 
#>    <int> <chr> <chr>
#>  1     1 a     <NA> 
#>  2     2 b     A    
#>  3     3 c     A    
#>  4     4 d     <NA> 
#>  5     5 e     <NA> 
#>  6     6 f     A    
#>  7     7 g     A    
#>  8     8 h     <NA> 
#>  9     9 i     <NA> 
#> 10    10 j     B    
#> 11    11 k     B    
#> 12    12 l     <NA>

Created on 2024-07-24 with reprex v2.0.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions