I got the output, but the total number of rows shrunk by about half (looks like it's doing unique values somehow), and it's producing columns as lists rather than individual doubles:
Lists are be created by pivot_wider() when deleting the values_from column results in duplicate rows:
library(tidyverse)
tibble(
ltr = rep(letters[1:2], each = 2),
num = 1:4
) -> small
small
#> # A tibble: 4 × 2
#> ltr num
#> <chr> <int>
#> 1 a 1
#> 2 a 2
#> 3 b 3
#> 4 b 4
small |>
pivot_wider(names_from = ltr, values_from = num)
#> Warning: Values from `num` are not uniquely identified; output will contain list-cols.
#> • Use `values_fn = list` to suppress this warning.
#> • Use `values_fn = {summary_fun}` to summarise duplicates.
#> • Use the following dplyr code to identify duplicates.
#> {data} |>
#> dplyr::summarise(n = dplyr::n(), .by = c(ltr)) |>
#> dplyr::filter(n > 1L)
#> # A tibble: 1 × 2
#> a b
#> <list> <list>
#> 1 <int [2]> <int [2]>
Yes, the contains() function allows you to match the names of the columns to strings: "i" to id, "n" to nestedvals, and 1:10 to the numbered columns, and the columns appear in the order specified.
An alternative is to use matches() instead, which requires regular expressions:
matches('i|n|[0-9]') # matches 'i' or 'n' or the digits 0-9
but then you may lose control of the of order in which the numbered columns appear.