Skip to content

More fill() options - fill-down-then-up, and fill-up-then-down #505

@coolbutuseless

Description

@coolbutuseless

Currently for missing data within groups, I do quite a lot of fill()ing down and then back up to densely fill in the missing values.

Depending upon number of groups and number of variables to replace, this can take a noticeable amount of time.

The current duplicate call to fill() could be avoided with additional options for .direction, i.e. downup and updown

An example of the new usage is shown below.

An implementation is included in this PR #504

suppressPackageStartupMessages({
  library(dplyr)
})

# Sample data
df <- data_frame(
  group = c( 1, 1,  1,  1,   2,  2,    2,  2),
  value = c(NA, 8, NA,  7,  NA, NA, 12.3, NA)
)

df
#> # A tibble: 8 x 2
#>   group value
#>   <dbl> <dbl>
#> 1     1  NA  
#> 2     1   8  
#> 3     1  NA  
#> 4     1   7  
#> 5     2  NA  
#> 6     2  NA  
#> 7     2  12.3
#> 8     2  NA

# Current call
df %>%
  group_by(group) %>%
  tidyr::fill(value, .direction = 'down') %>%
  tidyr::fill(value, .direction = 'up'  ) %>%
  ungroup()
#> # A tibble: 8 x 2
#>   group value
#>   <dbl> <dbl>
#> 1     1   8  
#> 2     1   8  
#> 3     1   8  
#> 4     1   7  
#> 5     2  12.3
#> 6     2  12.3
#> 7     2  12.3
#> 8     2  12.3

# Suggested call with new new 'fill()' options for '.direction'
df %>%
  group_by(group) %>%
  tidyr::fill(value, .direction = 'downup') %>%
  ungroup()
#> # A tibble: 8 x 2
#>   group value
#>   <dbl> <dbl>
#> 1     1   8  
#> 2     1   8  
#> 3     1   8  
#> 4     1   7  
#> 5     2  12.3
#> 6     2  12.3
#> 7     2  12.3
#> 8     2  12.3

Created on 2018-10-16 by the reprex
package
(v0.2.0).

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancementpivoting ♻️pivot rectangular data to different "shapes"wipwork in progress

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions