custom R function with group argument does not work while using the filter

Here is the programme which i am using to summarize the adsl data by groups TRT01PN and SITEID, however i get the error and it is from filter, so basically if more than 1 group variable is used and if we try to filter then its not working, i am not sure of the reason, here is the code i tried with sample data

filter(if_all(all_of(group), ~ !is.na(.)))


Error in `map2()`:
ℹ In index: 1.
Caused by error in `filter()`:
ℹ In argument: `if_all(all_of(valid_group), ~!is.na(.))`.
Caused by error in `if_all()`:
ℹ In argument: `all_of(valid_group)`.
Caused by error in `all_of()`:
! Can't subset elements that don't exist.
✖ Elements `TRT01PN` and `SITEID` don't exist.

use the data from tidyCDISC package

this is what is tried

library(tidyverse)
library(tidyCDISC)

# data
adsl <- tidyCDISC::adsl |> select(SITEID, TRT01PN, AGE, TRTDURD, AVGDD)

# function to summarize the numeric column 

adsl_summ <- function(.data, group=NULL, vars=NULL){
  #browser()
  group_syms <- syms(group)
  vars <- syms(vars)

  adsl <- .data |> select(!!!group_syms, !!!vars)
  
  var_name <- adsl |> select(- all_of(as.character(group_syms))) |> names()
  
adsl_list <- map(var_name, \(x) adsl |> select(!!!group_syms, all_of(x)))

map2_dfr(adsl_list, var_name, \(x,y) {
  
  ynam <- sym(y)

adsl |> group_by(!!!group_syms) |>  filter(if_all(all_of(group), ~ !is.na(.))) |>
  summarise(mean=mean(!!ynam),
  sd=sd(!!ynam),
  median=median(!!ynam),
  min=min(!!ynam),
  max=max(!!ynam)
  ) |> ungroup() |> mutate(name=y)

})

}

adsl_summ(adsl, group=c('TRT01PN','SITEID'), vars=c('AGE', 'TRTDURD', 'AVGDD'))

I update the code to put filter(if_all(all_of(group), ~ !is.na(.))) before group_by(!!!group_syms) and it resolved the issue

This topic was automatically closed 7 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.