I have data frame like
library(rstatix)
grp1 <- runif(10, min = 0, max = 100) # Exemple de données numériques
grp2 <- runif(10, min = 0, max = 100)
grp3 <- runif(10, min = 0, max = 100)
grp4 <- runif(10, min = 0, max = 100)
status <- sample(c("alive", "death", "sick"), 10, replace = TRUE) # Exemple de données caractère
my_data_frame <- data.frame(grp1, grp2, grp3, grp4, status)
and I want to do for each group.
wilcox_test(grp1~ status, p.adjust.method = "fdr")
I try
for (i in 4) {
var <- names(my_data_frame)
test <- my_data_frame %>% wilcox_test(var[i] ~ status, p.adjust.method = "fdr")}
but Column var[i]
doesn't exist.
with other test like glm this syntax works
rstatix
is designed to fit nicely into Tidyverse-based workflows and pipelines, so instead of a loop, you could first pivot your dataset to longer, group and pass grouped tibble to wilcox_test()
:
library(rstatix)
library(dplyr)
library(tidyr)
grp1 <- runif(10, min = 0, max = 100) # Exemple de données numériques
grp2 <- runif(10, min = 0, max = 100)
grp3 <- runif(10, min = 0, max = 100)
grp4 <- runif(10, min = 0, max = 100)
status <- sample(c("alive", "death", "sick"), 10, replace = TRUE) # Exemple de données caractère
my_data_frame <- data.frame(grp1, grp2, grp3, grp4, status)
my_data_frame |>
pivot_longer(-status, names_to = "grp") |>
group_by(grp) |>
wilcox_test(value ~ status, p.adjust.method = "fdr")
#> # A tibble: 12 × 10
#> grp .y. group1 group2 n1 n2 statistic p p.adj p.adj.signif
#> * <chr> <chr> <chr> <chr> <int> <int> <dbl> <dbl> <dbl> <chr>
#> 1 grp1 value alive death 3 3 3 0.7 0.857 ns
#> 2 grp1 value alive sick 3 4 3 0.4 0.857 ns
#> 3 grp1 value death sick 3 4 5 0.857 0.857 ns
#> 4 grp2 value alive death 3 3 3 0.7 0.7 ns
#> 5 grp2 value alive sick 3 4 8 0.629 0.7 ns
#> 6 grp2 value death sick 3 4 12 0.057 0.171 ns
#> 7 grp3 value alive death 3 3 7 0.4 0.629 ns
#> 8 grp3 value alive sick 3 4 8 0.629 0.629 ns
#> 9 grp3 value death sick 3 4 4 0.629 0.629 ns
#> 10 grp4 value alive death 3 3 3 0.7 0.7 ns
#> 11 grp4 value alive sick 3 4 4 0.629 0.7 ns
#> 12 grp4 value death sick 3 4 8 0.629 0.7 ns
Created on 2024-02-09 with reprex v2.1.0