Skip to content

missing documentation for passing additional arguments to values_fn in pivot_wider() #1114

@pinusm

Description

@pinusm

I was trying to figure out how to pass additional arguments to a function used in values_fn in pivot_wider().
Looking at the documentation I couldn't find any clues.
I tried several methods, inspired by what I did find in the documentation ({.value}), and other tidyverse functions (purrr, across).
I ended up with two methods that did work, using an unnamed function, and a predefined custom function, so the coding issue is solved, but the documentation is lacking..
Posting here so others could find the solution. Also, in case someone would like to change the functionality and accept additional syntax for the arguments of values_fn.

Others that came across this issue: https://community.rstudio.com/t/values-fn-length-in-pivot-wider-gives-null-length-na-instead-of-zero/95976

The reprex shows what works, and what doesn't..

library(tidyverse)
dx<-data.frame(A = c("B1","B2","B1","B2"),B = c(1,2,2,NA))
dx
#>    A  B
#> 1 B1  1
#> 2 B2  2
#> 3 B1  2
#> 4 B2 NA

dx %>%
  pivot_wider(
    names_from = A,
    values_from = B,
    values_fn = mean
  )
#> # A tibble: 1 x 2
#>      B1    B2
#>   <dbl> <dbl>
#> 1   1.5    NA

# the solution: an unnamed function
dx %>%
  pivot_wider(
    names_from = A,
    values_from = B,
    values_fn = function(x){mean(x,na.rm = T)}
  )
#> # A tibble: 1 x 2
#>      B1    B2
#>   <dbl> <dbl>
#> 1   1.5     2

# workaround using custom function
my_mean <- function(x){
  mean(x,na.rm = T)
}

dx %>%
  pivot_wider(
    names_from = A,
    values_from = B,
    values_fn = my_mean
  )
#> # A tibble: 1 x 2
#>      B1    B2
#>   <dbl> <dbl>
#> 1   1.5     2


# doesn't work
## dot-dot-dot
dx %>%
  pivot_wider(
    names_from = A,
    values_from = B,
    values_fn = mean,
    na.rm = T
  )
#> Error: 1 components of `...` were not used.
#> 
#> We detected these problematic arguments:
#> * `na.rm`
#> 
#> Did you misspecify an argument?

## purrr style
dx %>%
  pivot_wider(
    names_from = A,
    values_from = B,
    values_fn = ~mean(.x,na.rm = T)
  )
#> Error: `values_fn` must be a NULL, a function, or a named list

## {.value}
dx %>%
  pivot_wider(
    names_from = A,
    values_from = B,
    values_fn = mean({.value},na.rm = T)
  )
#> Error in mean({: object '.value' not found

Created on 2021-04-12 by the reprex package (v1.0.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviordocumentationpivoting ♻️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