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

Handling missing values with tidyr::fill() #73

Closed
ghost opened this issue Nov 20, 2018 · 1 comment
Closed

Handling missing values with tidyr::fill() #73

ghost opened this issue Nov 20, 2018 · 1 comment
Labels

Comments

@ghost
Copy link

ghost commented Nov 20, 2018

If missing value appears first in its key and it needed to be replaced by last observed value, it doesn't leave it with NA and cares last observed value from previous key (group).

harvest <- tsibble(
  year = c(2011, 2013, 2014, 2010, 2012, 2014),
  fruit = rep(c("kiwi", "cherry"), each = 3),
  kilo = sample(1:10, size = 6),
  key = id(fruit), index = year
)

harvest_fill <- fill_na(harvest, .full = TRUE)

harvest_fill %>%
  group_by_key() %>% # I've tried also group_by
  tidyr::fill(kilo, .direction = "down")

2010 kiwi observation need to be leaved with NA. I accomplished it by:

harvest_correct_fill <- 
  harvest_fill %>% 
    as.data.frame() %>% 
    group_by(fruit) %>% 
    tidyr::fill(kilo, .direction = "down")
@earowang
Copy link
Member

Thanks. I've added fill.grouped_ts to fix the issue. And fill() is re-exported from tidyr, so we can remove tidyr::.

library(tsibble)
harvest <- tsibble(
  year = c(2011, 2013, 2014, 2010, 2012, 2014),
  fruit = rep(c("kiwi", "cherry"), each = 3),
  kilo = sample(1:10, size = 6),
  key = id(fruit), index = year
)

harvest_fill <- fill_gaps(harvest, .full = TRUE)
harvest_fill %>%
  group_by_key() %>%
  fill(kilo, .direction = "down")
#> # A tsibble: 10 x 3 [1Y]
#> # Key:       fruit [2]
#> # Groups:    fruit [2]
#>     year fruit   kilo
#>    <dbl> <chr>  <int>
#>  1  2010 cherry     9
#>  2  2011 cherry     9
#>  3  2012 cherry     6
#>  4  2013 cherry     6
#>  5  2014 cherry     2
#>  6  2010 kiwi      NA
#>  7  2011 kiwi       1
#>  8  2012 kiwi       1
#>  9  2013 kiwi       4
#> 10  2014 kiwi       7

Created on 2018-11-21 by the reprex package (v0.2.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant