tidyr icon indicating copy to clipboard operation
tidyr copied to clipboard

convert option for separate_wider_*

Open paulstillman opened this issue 2 years ago • 2 comments

One of the most useful functions of the now superseded separate was the convert = TRUE option that would convert the resulting new columns into, for instance, numeric vectors. So the following:

df <- tibble(a = '1,2,3')
df |> 
  separate(a, into = c('num1', 'num2', 'num3'), sep = ',', convert = TRUE)

outputs as numeric columns:

   num1  num2  num3
  <int> <int> <int>
1     1     2     3

If we do the equivalent separate_wider_delim call:

df |> 
  separate_wider_delim(a, names = c('num1', 'num2', 'num3'), delim = ',')

the output is all character outputs:

  num1  num2  num3 
  <chr> <chr> <chr>
1 1     2     3    

Would it be possible to re-introduce the convert option to separate_wider_delim?

paulstillman avatar Sep 01 '23 22:09 paulstillman

I think it would be reasonable to introduce names_transform to the separate_wider_*() family, like we have for pivot_longer() (it is a more precise version of convert)

DavisVaughan avatar Sep 06 '23 13:09 DavisVaughan