I want to perform a kruskal_test
for multiple variables. I have been able to do this with anova tests as follows:
vars = c("Sepal.Length" , "Sepal.Width" ,"Petal.Length" , "Petal.Width")
for (i in vars) {
f <- reformulate(paste(i,'~Species'))
test = aov(data = iris, f )
print(i)
print(summary(test))
But I can't seem to use the same method for rstatix::kruskal_test
or kruskal.test
:
for (i in vars) {
f <- reformulate(paste(i,'~Species'))
test = kruskal.test(data = iris,formula= f )
test = kruskal_test(data = iris,formula= f )
print(i)
print(summary(test))
}
I get errors: Error in kruskal.test.default(data = iris, formula = f) : argument "x" is missing, with no default
or
Error in kruskal.test.formula(formula, data = data, ...) : 'formula' missing or incorrect
In
aov
you need this kind of formula,whereas in
kruskal.test
, it'sThe generic function appears to have an unexpected issue in its method dispatch mechanism, indicating a possible bug in its method selection process (R 4.3.2). Even though the arguments are named, the order matters (default order is
kruskal.test(formula, data)
, we havekruskal.test(data, formula)
):However:
It works if you use the method directly
Better use
lapply
though: