SebKrantz / collapse

Advanced and Fast Data Transformation in R
https://sebkrantz.github.io/collapse/
Other
651 stars 34 forks source link

Column selecting when using pivot() or across() #586

Closed ttrodrigz closed 4 months ago

ttrodrigz commented 4 months ago

This is more of a question, not an issue. I am wondering if there is a collapse equivalent to a tidyselect style of selecting variables within functions such as pivot() or fsummarise(..., across()).

Here are some things, for example, I do on a very regular basis using dplyr or tidyr:

pivot_longer(
    data = iris,
    cols = where(is.numeric)
)

pivot_longer(
    data = iris,
    cols = matches("\\.")
)

summarise(
    iris,
    across(
        .cols = where(is.numeric),
        .fns = fmean
    )
)

Is there a way within collapse for variable selecting by a logical condition or via regex? I know that the gvr() function allows for selecting of columns by regex, but I don't know if that can be paired with other functions?

Thanks for all the great work, I love the collapse packge.

SebKrantz commented 4 months ago

Regex is more difficult, you can only use a compound pipe. The others are easier, you can directly use the selector function:

library(fastverse)

pivot(iris, values = is.numeric)

iris %>% pivot(values = grep("\\.", names(.)))

fsummarise(
  iris,
  across(
    .cols = is.numeric,
    .fns = fmean
  )
)

The latter is redundant though. the collapse way would be

iris |> num_vars() |> fmean(drop = FALSE)
ttrodrigz commented 4 months ago

Understood, thank you! The collapse version of the summarise operation is slick.