mercredi 1 septembre 2021

Conditional group_by with example

I have a task to identify unique trial (1,2,3,...) in a dataset. Here is an example:

"source","ID","cultivar","design"
"PDMR_vol_12","CF027","Ambassador","RCBD"
"PDMR_vol_12","CF027","Ambassador","RCBD"
"PDMR_vol_12","CF027","Ambassador","RCBD"
"PDMR_vol_12","CF027","Ambassador","RCBD"
"PDMR_vol_7","CF026","ASG2000","RCBD"
"PDMR_vol_7","CF026","ASG2000","RCBD"
"PDMR_vol_7","CF026","ASG2000","RCBD"
"PDMR_vol_7","CF026","P26R61","RCBD"
"PDMR_vol_7","CF026","P26R61","RCBD"
"PDMR_vol_7","CF026","P26R61","RCBD"
"PDMR_vol_4","CF011","Roane","SP"
"PDMR_vol_4","CF011","Roane","SP"
"PDMR_vol_4","CF011","Tomahawk","SP"
"PDMR_vol_4","CF011","Tomahawk","SP"
"PDMR_vol_4","CF011","Everest","SP"
"PDMR_vol_4","CF011","Everest","SP"

The conditional columns are:

unique_trials_RCBD<- ("source","ID","cultivar","design")

unique_trials_SP<-unique_trials_RCBD[-3]

Using a conditional group_by based on a few columns, we almost get the correct result, with the exception that it does not correctly identify (PDMR_vol_7 CF026) as two trials.

doAGroupBy <- function(data, some_condition) {

 if (some_condition == TRUE) {

   group_args <- unique_trials_RCBD

  } else {

   group_args <- unique_trials_SP

 }

  data %>%
    group_by_at(vars(group_args))
}


 a<-doAGroupBy(data, FALSE) %>% 
   mutate(trial_number=cur_group_id())

In total, there should be 4 trials there. Any ideas on how to improve this code? thanks

Aucun commentaire:

Enregistrer un commentaire