Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
coolbutuseless opened this issue Oct 16, 2018 · 0 comments
Closed

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

coolbutuseless opened this issue Oct 16, 2018 · 0 comments

Comments

@coolbutuseless
Copy link
Contributor

@coolbutuseless coolbutuseless commented Oct 16, 2018

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.