[英]Problem with using tukey_hsd() inside a function and add_xy_position() in R
I have a problem with add_xy_position()
after using tukey_hsd()
from the rstatix package inside a function.我有一个问题
add_xy_position()
使用后tukey_hsd()
从一个函数内部的rstatix包。 This is what my function looks like:这是我的函数的样子:
make_tukey_test <- function (data,variable,grouping_variable){
data %>%
tukey_hsd(variable ~ grouping_variable)
}
When I call the function using the following code, it works perfectly fine and the test results are being saved in the data.frame:当我使用以下代码调用该函数时,它运行良好,并且测试结果保存在 data.frame 中:
test <- make_tukey_test(data = dat, variable = dat$num_var, grouping_variable = dat$factor_var)
However, when I try to add the x and y coordinates of dat$factor_var
using add_xy_position()
like this:但是,当我尝试使用
add_xy_position()
添加dat$factor_var
的 x 和 y 坐标时,如下所示:
test <- test %>%
add_xy_position(x = “factor_var”)
I get the following error message:我收到以下错误消息:
Error: Must group by variables found in `.data`. * Column `grouping_variable` is not found.
However, when I use tukey_hsd()
outside of my function, the code works perfectly fine and the coordinates are being added to the data.frame.但是,当我在函数之外使用
tukey_hsd()
,代码运行良好,并且坐标被添加到 data.frame 中。
I would greatly appreciate some helpful suggestions, as I have no clue why the code doesn't work when I´m using it inside my function.我非常感谢一些有用的建议,因为我不知道为什么当我在我的函数中使用它时代码不起作用。
I would suggest to pass column names as strings.我建议将列名作为字符串传递。 Since
tukey_hsd
accepts a formula object you can use reformulate
to create that and pass the grouping_variable
as it is in add_xy_position
function.由于
tukey_hsd
接受式对象可以使用reformulate
来创建和传递grouping_variable
因为它是在add_xy_position
功能。
Here is an example with mtcars
dataset.这是一个带有
mtcars
数据集的示例。
library(rstatix)
library(dplyr)
dat <- mtcars %>% mutate(cyl = factor(cyl))
make_tukey_test <- function (data,variable,grouping_variable){
data %>%
tukey_hsd(reformulate(grouping_variable, variable)) %>%
add_xy_position(x = grouping_variable)
}
test <- make_tukey_test(data = dat, variable = "mpg", grouping_variable = "cyl")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.