gpt4 book ai didi

r - 使函数适应 tidyverse 生态系统

转载 作者:行者123 更新时间:2023-12-04 15:05:11 28 4
gpt4 key购买 nike

library(rstatix) 中的 box_m 函数目前要求其第一个参数不包含分组变量,其第二个参数仅包含分组变量。例如:box_m(d[-1], d$Group)

我正在尝试重写此函数,使 box_m2 的工作方式如下:box_m2(d, Group)

我尝试了以下方法但没有成功,但想知道是否有办法实现我的目标?

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)

最佳答案

您可以借助 curly-curly ({{}}) 运算符编写函数。

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

关于r - 使函数适应 tidyverse 生态系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66286649/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com