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

make fill argument in complete work similarly to value_fill in pivot: assume everything() if a single value is passed #1111

Closed
luispfonseca opened this issue Mar 24, 2021 · 2 comments
Labels
feature a feature request or enhancement pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@luispfonseca
Copy link

Is it possible to make the fill argument in complete work the same way as the value_fill argument in pivot_wider, i.e., if only a single value is passed, assume this applies to all variables? This would make it easier to deal with many variables, without having to specifically write a list with every single variable and the value I want to fill in with.

Apologies if I have missed a way to actually do this. In any case, consistency across the two functions could be useful.

library(dplyr)
#> 
#> 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
library(tidyr) #1.1.3

# with values_fill in pivot_wider, if I provide a single value, default is to assume propagation to all variables
pivot_wider(fish_encounters, names_from = station, values_from = seen, values_fill = 2)
#> # A tibble: 19 x 12
#>    fish  Release I80_1 Lisbon  Rstr Base_TD   BCE   BCW  BCE2  BCW2   MAE   MAW
#>    <fct>   <int> <int>  <int> <int>   <int> <int> <int> <int> <int> <int> <int>
#>  1 4842        1     1      1     1       1     1     1     1     1     1     1
#>  2 4843        1     1      1     1       1     1     1     1     1     1     1
#>  3 4844        1     1      1     1       1     1     1     1     1     1     1
#>  4 4845        1     1      1     1       1     2     2     2     2     2     2
#>  5 4847        1     1      1     2       2     2     2     2     2     2     2
#>  6 4848        1     1      1     1       2     2     2     2     2     2     2
#>  7 4849        1     1      2     2       2     2     2     2     2     2     2
#>  8 4850        1     1      2     1       1     1     1     2     2     2     2
#>  9 4851        1     1      2     2       2     2     2     2     2     2     2
#> 10 4854        1     1      2     2       2     2     2     2     2     2     2
#> 11 4855        1     1      1     1       1     2     2     2     2     2     2
#> 12 4857        1     1      1     1       1     1     1     1     1     2     2
#> 13 4858        1     1      1     1       1     1     1     1     1     1     1
#> 14 4859        1     1      1     1       1     2     2     2     2     2     2
#> 15 4861        1     1      1     1       1     1     1     1     1     1     1
#> 16 4862        1     1      1     1       1     1     1     1     1     2     2
#> 17 4863        1     1      2     2       2     2     2     2     2     2     2
#> 18 4864        1     1      2     2       2     2     2     2     2     2     2
#> 19 4865        1     1      1     2       2     2     2     2     2     2     2

# fill argument in complete does not work the same way

# error
complete(fish_encounters, station, fish, fill = 2)
#> Error in replace_na.data.frame(full, replace = fill): is_list(replace) is not TRUE

# appears to work, but doesn't..
complete(fish_encounters, station, fish, fill = list(2)) %>% filter(seen == 2)
#> # A tibble: 0 x 3
#> # … with 3 variables: station <fct>, fish <fct>, seen <int>

# ..they're NAs
complete(fish_encounters, station, fish, fill = list(2)) %>% filter(is.na(seen))
#> # A tibble: 95 x 3
#>    station fish   seen
#>    <fct>   <fct> <int>
#>  1 Lisbon  4849     NA
#>  2 Lisbon  4850     NA
#>  3 Lisbon  4851     NA
#>  4 Lisbon  4854     NA
#>  5 Lisbon  4863     NA
#>  6 Lisbon  4864     NA
#>  7 Rstr    4847     NA
#>  8 Rstr    4849     NA
#>  9 Rstr    4851     NA
#> 10 Rstr    4854     NA
#> # … with 85 more rows

# works if i name the variable individually, but cumbersome when I have many variables
complete(fish_encounters, station, fish, fill = list(seen = 2)) %>% filter(seen == 2)
#> # A tibble: 95 x 3
#>    station fish   seen
#>    <fct>   <fct> <dbl>
#>  1 Lisbon  4849      2
#>  2 Lisbon  4850      2
#>  3 Lisbon  4851      2
#>  4 Lisbon  4854      2
#>  5 Lisbon  4863      2
#>  6 Lisbon  4864      2
#>  7 Rstr    4847      2
#>  8 Rstr    4849      2
#>  9 Rstr    4851      2
#> 10 Rstr    4854      2
#> # … with 85 more rows

Created on 2021-03-24 by the reprex package (v0.3.0)

@hadley hadley added feature a feature request or enhancement pivoting ♻️ pivot rectangular data to different "shapes" labels Aug 23, 2021
@hadley
Copy link
Member

hadley commented Aug 23, 2021

Duplicate of #1108

@hadley hadley marked this as a duplicate of #1108 Aug 23, 2021
@hadley hadley closed this as completed Aug 23, 2021
@hadley
Copy link
Member

hadley commented Aug 23, 2021

(since this is implemented via replace_na())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

No branches or pull requests

2 participants