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)
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
We could also convert to symbol with ensym
and evaluate ( !!
). In that way, it is flexible to either pass unquoted or quoted arguments
library(rstatix)
library(dplyr)
library(rstatix)
box_m2 <- function(data, group){
group <- ensym(group)
dat <- dplyr::select(data, -!!group)
box_m(dat, data %>%
pull(!! group))
}
-testing
identical(box_m(d[-1], d$Group), box_m2(d, Group))
#[1] TRUE
identical(box_m(d[-1], d$Group), box_m2(d, "Group"))
#[1] TRUE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.