How to nest numeric values into matrices instead of tibbles

I'm reading numeric force signals from multiple sensors and nesting
the forces since I later need to manipulate the force arrays with
matrix algebra. Here a simplified example:

 library(tidyverse)
  library(vroom)
  vroom( I( "Fx_1, Fy_1, Fx_2, Fy_2\n
  1, 0, 0, 1\n
  3, 4, 0, 4\n
  6, 7, 8, 5"), delim = ",",
  show_col_types = FALSE) %>%
    pivot_longer( everything(),
                 names_pattern = "(F.)_(.)",
                 names_to=c( ".value", "Sensor")) %>%
    nest( F = c( Fx, Fy))
#> # A tibble: 2 × 2
#>   Sensor F               
#>   <chr>  <list>          
#> 1 1      <tibble [3 × 2]>
#> 2 2      <tibble [3 × 2]>

With above structure I need to map as.matrix to F in a further step to be able to apply %*% and cross.
Isn't it possible to nest the values directly into matrices instead of tibbles? Why is there no template option to force the column type other than tibble?

Thanks for tidyverse!

My perception is that tidyverse is a toolset primarily oriented to data.frames , and not particularly matrices.
The tidyr description says Nesting creates a list-column of data frames
You could write your own code to modify tidyr to work in the way you prefer or ask the tidyr developers to do so.
It seems like your map to matrix is fairly straightforward and a good way to do your work though, if I was in your shoes I would probably just continue adding that map statement and keep this flow

@nirgrahamuk you're right about the data.frame or tibble orientation of tidyverse, but tibbles are capable of "carrying" matrices and not exclusively data.frames. And I'd like to take advantage of this capability so I don't have to program matrix operations for tibbles.

I was inspired by the unnest option ptype:
Optionally, a named list of column name-prototype pairs to coerce cols to, overriding the default that will be guessed from combining the individual values

I wish such an option for nest would exist so, I imagine, it would be possible to nest some tibble columns directly into matrices!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.