nacnudus / unpivotr

Unpivot complex and irregular data layouts in R
https://nacnudus.github.io/unpivotr/
Other
185 stars 19 forks source link

Allow behead() to take a vector for the name argument #45

Closed billdenney closed 3 years ago

billdenney commented 3 years ago

Related to #42 (and maybe it's a duplicate of #42, but I'm not sure).

I often have data with multiple rows or columns of header-type information to the sides. A common case for me would look like:

  Bill Jane
  male female
12 0.1 0.2
13 0.3 0.4

And, I'd like to be able to extract multiple behead()s in one call so that my code does not become many sequential behead() calls. For the example above, it would look like:

my_data %>%
  behead(direction="up", name=c("Name", "Sex")) %>%
  behead(direction="left", name="Age")

Or perhaps for extra flexibility (which may be more than is desired:

my_data %>%
  behead(direction=c("up", "up", "left"), name=c("Name", "Sex", "Age"))

I'm not sure if I like the second example because it may lead to less-readable code, overall.

If this would be a feature of interest, I'm happy to make a PR for it.

nacnudus commented 3 years ago

Thank you for this suggestion. I think #42 is essentially the same idea.

I'm reluctant to do it for a couple of reasons.

One reason is that all the other arguments would also have to accept vectors, and the formatters argument is already a list, so would become a list of lists.

The other reason is that I think it's important to retain the concept of behead() stripping exactly one layer at a time, because it makes debugging easier, and I think users are more likely to realise that the order of operations matters. It also allows the locatr package to work, and is consistent with the reverse operation of the mmtable2 package (constructing layered tables).

In other words, the repetitive behead()s are, I think, a feature! Before I wrote this package, there were others that did similar things with a single call, which ended up constraining them, so for unpivotr I adopted the dplyr style of sequencing simple, incremental transformations.

billdenney commented 3 years ago

Thanks for the follow-up. It makes sense as a feature.