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

Allow single values_fill in pivot_wider() #746

Closed
adisarid opened this issue Sep 17, 2019 · 1 comment
Closed

Allow single values_fill in pivot_wider() #746

adisarid opened this issue Sep 17, 2019 · 1 comment
Labels
feature pivoting ♻️

Comments

@adisarid
Copy link

@adisarid adisarid commented Sep 17, 2019

This is a feature suggestion inspired by how the fill argument was used in spread.

Currently, in pivot_wider, when you want to "fill" non-existing values, you need to provide a named list for each original column (what fill value should be used). It is common to use the same fill value for all column (and sometimes, its just a single column we're widening).

I suggest that if the user provides a value instead of a named list, the function will apply this value on all cases (as if it was specified with a named list containing all the value columns).

By example:

to_wider <- tribble(
  ~id, ~names_from_here, ~values_from_here,
  1, "col_a", 10,
  1, "col_b", 20,
  2, "col_b", 30
)

# this works fine:
pivot_wider(to_wider, 
   names_from = names_from_here, values_from = values_from_here, 
   values_fill = list(values_from_here = -99))

# A tibble: 2 x 3
#     id col_a col_b
#  <dbl> <dbl> <dbl>
#1     1    10    20
#2     2   -99    30


# this yields an error (but I expected it to work and yield the same output as the previous call)
pivot_wider(to_wider, 
   names_from = names_from_here, values_from = values_from_here, 
   values_fill = -99)

# Error in values_fill[[value]] : subscript out of bounds

This suggestion slightly resembles issue #739 (which suggests a similar behavior for the values_fn argument).

@DataStrategist

This comment has been minimized.

@hadley hadley changed the title In pivot_wider, when the values_fill is provided as a value instead of a named list, apply it on all values_from columns Allow single values_fill in pivot_wider() Nov 24, 2019
@hadley hadley added feature pivoting ♻️ labels Nov 24, 2019
@hadley hadley closed this as completed in e7ffbd4 Nov 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature pivoting ♻️
Projects
None yet
Development

No branches or pull requests

3 participants