Open caparks2 opened 3 years ago
I can confirm this one. Just got me, slightly simpler example
packageVersion("rstatix")
[1] ‘0.7.0’
# Doesn;t work (and throws mysterious error)
iris %>% filter(Species != "setosa") %>% rstatix::t_test(Sepal.Length ~ Species)
# Works:
t.test(Sepal.Length ~ Species, iris %>% filter(Species != "setosa"))
# Work around:
iris %>% filter(Species != "setosa") %>%
mutate(across(where(is.factor), forcats::fct_drop)) %>%
rstatix::t_test(Sepal.Length ~ Species)
Also noticed this today. I think it would be good if the function would still work with unused factor levels. Maybe the function could automatically drop unused factor levels. Potentially with a warning. With the current error message, I found it quite tricky to figure out, what was going wrong.
I ran into this and arrived at same conclusion about unused factor levels. Here's another minimal example, with wilcox_test() using the built-in starwars data object with rstatix v 0.7.2:
> packageVersion("rstatix")
[1] ‘0.7.2’
library(tidyverse)
library(rstatix)
# use built-in starwars tibble
sw <- starwars
# cast sex variable (currently chr type) to factor
sw$sex <- as.factor(sw$sex)
# how many characters of different sexes?
sw %>% group_by(sex) %>% summarize(n=n())
# let's just keep male and female characters for this analysis
sw <- filter(sw, str_detect(sex, "ale"))
# run a wilcoxon test: height as a function of sex
# this errors
sw %>% wilcox_test(height ~ sex)
# drop unused levels from factor
sw$sex <- droplevels(sw$sex)
# no longer errors
sw %>% wilcox_test(height ~ sex)
In the following example, base R
stats::t.test()
works, butrstatix::t_test()
doesn't. I think the reason for the error is because of unused factor levels.If I manually fix the unused factor levels,
rstatix::t_test()
now works.