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

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

Closed
pinusm opened this issue Apr 12, 2021 · 3 comments · Fixed by #1232
Closed

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

pinusm opened this issue Apr 12, 2021 · 3 comments · Fixed by #1232
Labels
bug an unexpected problem or unintended behavior documentation pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@pinusm
Copy link

pinusm commented Apr 12, 2021

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)

@Close-your-eyes
Copy link

Very useful to me, thanks a lot.
I wanted to collapse character values and ended up using this function to pass to values_fn:

collapse.fun <- function(x){ paste(x,collapse = ", ") }

@hadley hadley added documentation pivoting ♻️ pivot rectangular data to different "shapes" labels Aug 23, 2021
@hadley
Copy link
Member

hadley commented Aug 23, 2021

I think we could just add an example here, probably using ~.

@DavisVaughan
Copy link
Member

First we have to make this work 😬

library(tidyverse)

dx <- data.frame(A = c("B1","B2","B1","B2"),B = c(1,2,2,NA))

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

Created on 2021-10-28 by the reprex package (v2.0.1)

@DavisVaughan DavisVaughan added the bug an unexpected problem or unintended behavior label Oct 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior documentation pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants