Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adapting a function to tidyverse ecosystem

Function box_m from library(rstatix) currently requires that its first argument NOT include the grouping variable, and its second argument only be the grouping variable. For example: box_m(d[-1], d$Group).

I'm trying to re-write this function such that box_m2 would work like: box_m2(d, Group).

I have tried the following without success but was wondering if there might be a way to achieve my goal?

library(rstatix)
library(tidyverse)

d <- read.csv("https://raw.githubusercontent.com/rnorouzian/v/main/memory.csv")[-1]

  box_m(d[-1], d$Group) # How the function currently works

 # box_m2(d, Group) # How I would like the function to work
 
  
# My trial without success to achieve `box_m2`:

  box_m2 <- function(data, group){
    
    dat <- dplyr::select(data, -vars(group))
    box_m(dat, one_of(group))
  }

# New function
  box_m2(d, Group) 
like image 578
rnorouzian Avatar asked Mar 08 '26 23:03

rnorouzian


1 Answers

You can write the function with the help of curly-curly ({{}}) operator.

library(rstatix)
library(dplyr)

box_m2 <- function(data, group){
  dat <- dplyr::select(data, -{{group}})
  box_m(dat, data %>% pull({{group}}))
}

identical(box_m(d[-1], d$Group), box_m2(d, Group))
#[1] TRUE
like image 137
Ronak Shah Avatar answered Mar 10 '26 13:03

Ronak Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!