Open prototaxites opened 4 months ago
I think that the implementation of this would be relatively straightforward since you could use vec_chop()
— you'd just have to figure out how to generate the right vector of index. And it'll require some thinking about the interface, since you might want to provide the number of groups, the group size, or an actual vector of group ids.
{purrr} provides
list_flatten()
, which takes a list and removes a single layer of hierarchy. However, it would be quite useful to be able to do the reverse, and "unflatten" a list or vector into a list of lists. While the exact inverse of a flatten operation is likely to be impossible, this could be usefully implemented by allowing the user to specify either some kind of grouping vector (a character string or numeric vector the same length as the list to "unflatten"), or a chunk size at which to aggregate.This would be useful in cases when a user has a list or vector, and a function that is able to operate on subsets of that vector, rather than solely individual elements, and especially when there might be a useful speed gain operating over chunks but not over the whole vector at once.
For example, in my case: I have some vector
x
of column indices of a matrix, and want to do some matrix multiplication. I can do this with a single operation without using purrr, but for a large matrix this is also likely to be slow. I can also do this column-wise by mapping overx
, but this can be slow depending on the number of columns. It would be useful to be able to splitx
into a list of equal-sized chunks to find an optimum chunk size for computation, before combining the final output with reduce. (note that in my case, the full computation is very slow as I am usingrvar
types from the {posterior} package rather than scalars)The proposal would add something like the following:
I see a similar function was proposed and closed here: https://github.com/tidyverse/purrr/issues/274, but I think this proposal differs in that it specifically is about splitting/unflattening lists rather than dataframe rows.