1

The starts_with("O") from tidyselect works well with dplyr::select function but not with collapse::fselect. Wondering if I'm missing some basic here.

library(collapse)
library(tidyverse)
wlddev %>% 
  select(starts_with("O")) %>% 
  head()
#>    OECD       ODA
#> 1 FALSE 116769997
#> 2 FALSE 232080002
#> 3 FALSE 112839996
#> 4 FALSE 237720001
#> 5 FALSE 295920013
#> 6 FALSE 341839996

wlddev %>% 
  fselect(starts_with("O")) %>% 
  head()
#> Error:
#> ! `starts_with()` must be used within a *selecting* function.
#> i See <https://tidyselect.r-lib.org/reference/faq-selection-context.html>.
dan1st
  • 12,568
  • 8
  • 34
  • 67
MYaseen208
  • 22,666
  • 37
  • 165
  • 309
  • Is it mentioned somewhere in `collapse` documentation that tidyselect variables (`starts_with`, `ends_with` etc) will work with `fselect` ? Why do you think it should work with `collapse::fselect` ? What exactly is weird here? – Ronak Shah Mar 05 '22 at 02:23
  • @RonakShah: Please have a quick look on [Regarding the Integration with dplyr, plm, data.table, sf and Other Classes](https://sebkrantz.github.io/collapse/index.html). – MYaseen208 Mar 05 '22 at 02:56
  • 1
    I am not sure what do you want me to take a look at exactly but from that link I found this page https://sebkrantz.github.io/collapse/articles/collapse_and_dplyr.html which is closer to what you are trying to do and again I don't see any example there which uses `start_with`/`ends_with` etc. Nor do I find any such example in `?fselect`, so I don't know if this is even possible. Moreover, I think this question is more suited as an issue on [`collapse` github page](https://github.com/SebKrantz/collapse/issues) perhaps, as a feature request ? – Ronak Shah Mar 05 '22 at 03:20

1 Answers1

1

We may use gvr

library(collapse)
wlddev %>% 
  gvr("^O") %>%
  head
   OECD       ODA
1 FALSE 116769997
2 FALSE 232080002
3 FALSE 112839996
4 FALSE 237720001
5 FALSE 295920013
6 FALSE 341839996

fsum can group in g

fsum(gvr(wlddev, "^O"), g = wlddev[["iso3c"]])
    OECD          ODA
ABW    0    664899998
AFG    0  89252909923
AGO    0  15512219971
ALB    0  10013700017
AND    0           NA
ARE    0    602289998
ARG    0   6415849996
ARM    0   8190379934
ASM    0           NA
ATG    0    420559999
AUS   61           NA
AUT   61           NA
AZE    0   5049070016
BDI    0  17767379852
BEL   61           NA
BEN    0  18904160029
...

Or with pipe

wlddev %>% 
  gvr("^O") %>%
  fsum(., g = slt(wlddev, iso3c))

-output

  OECD          ODA
ABW    0    664899998
AFG    0  89252909923
AGO    0  15512219971
ALB    0  10013700017
AND    0           NA
ARE    0    602289998
...

Or may use

wlddev %>% 
  fgroup_by(iso3c) %>% 
  gvr("^O") %>% 
  fsum

-output

   iso3c OECD          ODA
1     ABW    0    664899998
2     AFG    0  89252909923
3     AGO    0  15512219971
4     ALB    0  10013700017
5     AND    0           NA
6     ARE    0    602289998
7     ARG    0   6415849996
8     ARM    0   8190379934
9     ASM    0           NA
10    ATG    0    420559999
...
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Thanks @akrun. This code works `wlddev %>% group_by(iso3c) %>% summarise(across(starts_with("O"), sum))` but not this one `wlddev %>% fgroup_by(iso3c) %>% fsummarise(across(.cols = gvr("^O"), .fns = fsum))`. Any thoughts. – MYaseen208 Mar 05 '22 at 17:30
  • @MYaseen208 do you need `fsum(gvr(wlddev, "^O"), g = wlddev[["iso3c"]])` – akrun Mar 05 '22 at 17:31
  • 1
    @MYaseen208 the issue seems to be that `across` is not comptabile with collapse functions – akrun Mar 05 '22 at 17:37
  • @MYaseen208 does this works for you `wlddev %>% fgroup_by(iso3c) %>% gvr("^O") %>% fsum()` – akrun Mar 05 '22 at 17:56
  • 2
    @MYaseen208 you need to read the collapse function documentation carefully to see what a function is intended for and what it supports. Just assuming collapse can do everything dplyr does in the same way or looking at those vignettes won't help much for specific things. For example in the [documentation of across](https://sebkrantz.github.io/collapse/reference/across.html) its says precisely what can be passed to the `.cols` argument. collapse does not support select helpers and I have no interest in implementing all of these small dplyr functions. – Sebastian Mar 05 '22 at 23:11