Example

We take the example from the first situation, but with the name of the values variable included in the wide table:

cases
## # A tibble: 3 × 3
##   country     cases_1999 cases_2000
##   <chr>            <dbl>      <dbl>
## 1 Afghanistan        745       2666
## 2 Brazil           37737      80488
## 3 China           212258     213766

We can now extract this name instead of defining it with values_to:

cases_long <- cases |> 
  pivot_longer(
    cols = !country,
    names_to = c(".value", "year"),
    names_sep = "_"
  )
cases_long
## # A tibble: 6 × 3
##   country     year   cases
##   <chr>       <chr>  <dbl>
## 1 Afghanistan 1999     745
## 2 Afghanistan 2000    2666
## 3 Brazil      1999   37737
## 4 Brazil      2000   80488
## 5 China       1999  212258
## 6 China       2000  213766
identical(cases_long, table_tidy)
## [1] TRUE

Recreate the wide table:

cases_long |> 
  pivot_wider(
    names_from = year,
    values_from = cases,
    names_glue = "{.value}_{year}"
  )
## # A tibble: 3 × 3
##   country     cases_1999 cases_2000
##   <chr>            <dbl>      <dbl>
## 1 Afghanistan        745       2666
## 2 Brazil           37737      80488
## 3 China           212258     213766