Problem with pivot_longer_spec

I am having problems with the pivot family of functions which don't seem to work. I have updated tidyr to version 1.0.2 have rlang version 0.4.5. I also re-installed R 4.0.0 so don't understand why this is not working. Here I am trying to collect metadata for the transformation I want to perform.

LomaWideSmall %>%
  tidyr::pivot_longer_spec(
    cols = starts_with("fight"), 
    names_to = "fight_no", 
    values_to = "result", 
    names_prefix = "fight_"
    )

Error in tidyr::pivot_longer_spec(., cols = starts_with("fight"), names_to = "fight_no", :
unused arguments (cols = starts_with("fight"), names_to = "fight_no", values_to = "result", names_prefix = "fight_")

Can anyone help?

The dataframe looks like this:

opponent date fight_1 fight_2 fight_3 fight_4 fight_5 fight_6
José Ramírez 12/10/2013 Win NA NA NA NA NA
Orlando Salido 01/03/2014 NA Loss NA NA NA NA
Gary Russell Jr. 21/06/2014 NA NA Win NA NA NA
Chonlatarn Piriyapinyo 22/11/2014 NA NA NA Win NA NA
Gamalier RodrĂ­guez 02/05/2015 NA NA NA NA Win NA
Romulo Koasicha 07/11/2015 NA NA NA NA NA Win

I think you might be trying to use pivot_longer() (or at least you're using the arguments for it) which does the actual pivoting, and not pivot_longer_spec(), which is part of a more advanced pipeline for manually defining a specification, and requires a special spec object:

library(tidyverse)

LomaWideSmall<- tibble::tribble(
                 ~opponent,        ~date, ~fight_1, ~fight_2, ~fight_3, ~fight_4, ~fight_5, ~fight_6,
            "José Ramírez", "12/10/2013",    "Win",       NA,       NA,       NA,       NA,       NA,
          "Orlando Salido", "01/03/2014",       NA,   "Loss",       NA,       NA,       NA,       NA,
        "Gary Russell Jr.", "21/06/2014",       NA,       NA,    "Win",       NA,       NA,       NA,
  "Chonlatarn Piriyapinyo", "22/11/2014",       NA,       NA,       NA,    "Win",       NA,       NA,
      "Gamalier RodrĂ­guez", "02/05/2015",       NA,       NA,       NA,       NA,    "Win",       NA,
         "Romulo Koasicha", "07/11/2015",       NA,       NA,       NA,       NA,       NA,    "Win"
  )

LomaWideSmall %>%
  tidyr::pivot_longer(
    cols = starts_with("fight"), 
    names_to = "fight_no", 
    values_to = "result", 
    names_prefix = "fight_"
  )
#> # A tibble: 36 x 4
#>    opponent       date       fight_no result
#>    <chr>          <chr>      <chr>    <chr> 
#>  1 José Ramírez   12/10/2013 1        Win   
#>  2 José Ramírez   12/10/2013 2        <NA>  
#>  3 José Ramírez   12/10/2013 3        <NA>  
#>  4 José Ramírez   12/10/2013 4        <NA>  
#>  5 José Ramírez   12/10/2013 5        <NA>  
#>  6 José Ramírez   12/10/2013 6        <NA>  
#>  7 Orlando Salido 01/03/2014 1        <NA>  
#>  8 Orlando Salido 01/03/2014 2        Loss  
#>  9 Orlando Salido 01/03/2014 3        <NA>  
#> 10 Orlando Salido 01/03/2014 4        <NA>  
#> # … with 26 more rows

Created on 2020-05-08 by the reprex package (v0.3.0.9001)

Thanks for your help Mara. I am actually trying to use pivot_longer_spec, to define a specification based on the tutorial here:

https://www.storybench.org/pivoting-data-from-columns-to-rows-and-back-in-the-tidyverse/

But not getting the desired result which should hopefully be:

.name .value fight_no
fight_1 result 1
fight_2 result 2
fight_3 result 3
fight_4 result 4
fight_5 result 5
fight_6 result 6

It turns out what I need is build_longer_spec. The result gets stored as a spec object and fight_no formatted as.numeric before being supplied to pivot_longer_spec.

1 Like

Yeah you need to build the spec object first!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.