Skip to content

Allow single values_fill in pivot_wider() #746

@adisarid

Description

@adisarid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions