Is there a way to quickly process hundreds of data with the kruskal-wallis test?

library(tidyverse)
library(readxl)

kegg <- read_csv(file = "raw_data/kegg relative.csv")
kegg %>% group_by(NAME) %>% summarise(n = n(), mean = mean(ko00072), sd = sd(ko00072))

kruskal.test(ko00072 ~ NAME, data = kegg)

This is my code, and I am doing the analysis one by one.

I need to analyze about 100 pieces of data, is there a way to do it all at once?

The jpg file is part of kegg (read file).

Reshape your data into a long format and nest by "ko*" variables.

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Actually, i am beginner of R software, using nest function is difficult to me.

type or paste code here

library(tidyverse)
library(readxl)
library(reprex)
library(datapasta)

kegg <- read_csv(file = "raw_data/kegg relative.csv")
kegg %>% group_by(NAME) %>%  summarise(n = n(), mean = mean(ko00072), sd = sd(ko00072))

kruskal.test(ko00072 ~ NAME, data = kegg)

type or paste code here
kegg <- tibble::tribble(
               ~NAME, ~ko00010, ~ko00020, ~ko00030, ~ko00040, ~ko00051, ~ko00052, ~ko00053, ~ko00061, ~ko00071,
               "CON",     1.09,      1.2,     1.57,     0.72,     0.95,     0.94,     0.22,     1.88,     0.38,
               "CON",     1.09,     1.22,     1.55,     0.73,     0.97,     0.97,     0.23,     1.86,     0.38,
               "CON",     1.09,     1.25,     1.58,     0.72,     0.96,     0.95,     0.22,     1.86,     0.39,
          "TRT-AANC",      1.1,     1.27,     1.57,     0.72,     0.96,     0.95,     0.23,     1.89,      0.4,
          "TRT-AANC",      1.1,     1.27,     1.57,     0.71,     0.94,     0.94,     0.22,     1.91,     0.39,
          "TRT-AANC",      1.1,     1.29,     1.58,     0.74,     0.97,     0.96,     0.23,     1.87,      0.4,
          "TRT-ATAX",      1.1,     1.22,     1.58,     0.74,     0.97,     0.98,     0.22,     1.87,     0.37,
          "TRT-ATAX",     1.09,     1.21,     1.58,     0.73,     0.97,     0.97,     0.22,     1.87,     0.38,
          "TRT-ATAX",     1.09,     1.23,     1.57,     0.72,     0.94,     0.94,     0.22,     1.89,     0.38,
          "TRT-CTEN",     1.09,     1.27,     1.57,     0.73,     0.96,     0.96,     0.23,     1.86,     0.39,
          "TRT-CTEN",     1.09,     1.27,     1.57,     0.72,     0.94,     0.94,     0.22,     1.87,     0.39,
          "TRT-CTEN",     1.09,     1.24,     1.58,     0.73,     0.97,     0.96,     0.22,     1.86,     0.39,
          "TRT-GELL",      1.1,     1.18,     1.56,     0.73,     0.95,     0.96,     0.22,     1.87,     0.37,
          "TRT-GELL",      1.1,     1.22,     1.54,     0.72,     0.95,     0.95,     0.22,     1.88,     0.38,
          "TRT-GELL",     1.09,     1.25,     1.55,     0.73,     0.96,     0.96,     0.22,     1.86,     0.39,
          "TRT-GPAR",     1.09,     1.23,     1.56,     0.74,     0.97,     0.97,     0.22,     1.84,     0.38,
          "TRT-GPAR",     1.08,     1.25,     1.57,     0.74,     0.97,     0.96,     0.22,     1.85,     0.38,
          "TRT-GPAR",     1.09,     1.28,     1.57,     0.73,     0.96,     0.95,     0.23,     1.87,     0.39
          )
head(kegg)

library(tidyverse)
library(readxl)
library(reprex)
library(datapasta)

kegg <- tibble::tribble(
~NAME, ~ko00010, ~ko00020, ~ko00030, ~ko00040, ~ko00051, ~ko00052, ~ko00053, ~ko00061, ~ko00071,
"CON", 1.09, 1.2, 1.57, 0.72, 0.95, 0.94, 0.22, 1.88, 0.38,
"CON", 1.09, 1.22, 1.55, 0.73, 0.97, 0.97, 0.23, 1.86, 0.38,
"CON", 1.09, 1.25, 1.58, 0.72, 0.96, 0.95, 0.22, 1.86, 0.39,
"TRT-AANC", 1.1, 1.27, 1.57, 0.72, 0.96, 0.95, 0.23, 1.89, 0.4,
"TRT-AANC", 1.1, 1.27, 1.57, 0.71, 0.94, 0.94, 0.22, 1.91, 0.39,
"TRT-AANC", 1.1, 1.29, 1.58, 0.74, 0.97, 0.96, 0.23, 1.87, 0.4,
"TRT-ATAX", 1.1, 1.22, 1.58, 0.74, 0.97, 0.98, 0.22, 1.87, 0.37,
"TRT-ATAX", 1.09, 1.21, 1.58, 0.73, 0.97, 0.97, 0.22, 1.87, 0.38,
"TRT-ATAX", 1.09, 1.23, 1.57, 0.72, 0.94, 0.94, 0.22, 1.89, 0.38,
"TRT-CTEN", 1.09, 1.27, 1.57, 0.73, 0.96, 0.96, 0.23, 1.86, 0.39,
"TRT-CTEN", 1.09, 1.27, 1.57, 0.72, 0.94, 0.94, 0.22, 1.87, 0.39,
"TRT-CTEN", 1.09, 1.24, 1.58, 0.73, 0.97, 0.96, 0.22, 1.86, 0.39,
"TRT-GELL", 1.1, 1.18, 1.56, 0.73, 0.95, 0.96, 0.22, 1.87, 0.37,
"TRT-GELL", 1.1, 1.22, 1.54, 0.72, 0.95, 0.95, 0.22, 1.88, 0.38,
"TRT-GELL", 1.09, 1.25, 1.55, 0.73, 0.96, 0.96, 0.22, 1.86, 0.39,
"TRT-GPAR", 1.09, 1.23, 1.56, 0.74, 0.97, 0.97, 0.22, 1.84, 0.38,
"TRT-GPAR", 1.08, 1.25, 1.57, 0.74, 0.97, 0.96, 0.22, 1.85, 0.38,
"TRT-GPAR", 1.09, 1.28, 1.57, 0.73, 0.96, 0.95, 0.23, 1.87, 0.39
)

head(kegg)
#> # A tibble: 6 x 10
#> NAME ko00010 ko00020 ko00030 ko00040 ko00051 ko00052 ko00053 ko00061 ko00071
#>
#> 1 CON 1.09 1.2 1.57 0.72 0.95 0.94 0.22 1.88 0.38
#> 2 CON 1.09 1.22 1.55 0.73 0.97 0.97 0.23 1.86 0.38
#> 3 CON 1.09 1.25 1.58 0.72 0.96 0.95 0.22 1.86 0.39
#> 4 TRT-A~ 1.1 1.27 1.57 0.72 0.96 0.95 0.23 1.89 0.4
#> 5 TRT-A~ 1.1 1.27 1.57 0.71 0.94 0.94 0.22 1.91 0.39
#> 6 TRT-A~ 1.1 1.29 1.58 0.74 0.97 0.96 0.23 1.87 0.4

I just copy bit of my data in CSV file.

Thanks!

Is not clear what your intended output would be but this would apply the test for all the "ko*" variables at once

library(tidyverse)

# Provided sample data in a copy/paste friendly format
kegg <- tibble::tribble(
    ~NAME, ~ko00010, ~ko00020, ~ko00030, ~ko00040, ~ko00051, ~ko00052, ~ko00053, ~ko00061, ~ko00071,
    "CON",     1.09,      1.2,     1.57,     0.72,     0.95,     0.94,     0.22,     1.88,     0.38,
    "CON",     1.09,     1.22,     1.55,     0.73,     0.97,     0.97,     0.23,     1.86,     0.38,
    "CON",     1.09,     1.25,     1.58,     0.72,     0.96,     0.95,     0.22,     1.86,     0.39,
    "TRT-AANC",      1.1,     1.27,     1.57,     0.72,     0.96,     0.95,     0.23,     1.89,      0.4,
    "TRT-AANC",      1.1,     1.27,     1.57,     0.71,     0.94,     0.94,     0.22,     1.91,     0.39,
    "TRT-AANC",      1.1,     1.29,     1.58,     0.74,     0.97,     0.96,     0.23,     1.87,      0.4,
    "TRT-ATAX",      1.1,     1.22,     1.58,     0.74,     0.97,     0.98,     0.22,     1.87,     0.37,
    "TRT-ATAX",     1.09,     1.21,     1.58,     0.73,     0.97,     0.97,     0.22,     1.87,     0.38,
    "TRT-ATAX",     1.09,     1.23,     1.57,     0.72,     0.94,     0.94,     0.22,     1.89,     0.38,
    "TRT-CTEN",     1.09,     1.27,     1.57,     0.73,     0.96,     0.96,     0.23,     1.86,     0.39,
    "TRT-CTEN",     1.09,     1.27,     1.57,     0.72,     0.94,     0.94,     0.22,     1.87,     0.39,
    "TRT-CTEN",     1.09,     1.24,     1.58,     0.73,     0.97,     0.96,     0.22,     1.86,     0.39,
    "TRT-GELL",      1.1,     1.18,     1.56,     0.73,     0.95,     0.96,     0.22,     1.87,     0.37,
    "TRT-GELL",      1.1,     1.22,     1.54,     0.72,     0.95,     0.95,     0.22,     1.88,     0.38,
    "TRT-GELL",     1.09,     1.25,     1.55,     0.73,     0.96,     0.96,     0.22,     1.86,     0.39,
    "TRT-GPAR",     1.09,     1.23,     1.56,     0.74,     0.97,     0.97,     0.22,     1.84,     0.38,
    "TRT-GPAR",     1.08,     1.25,     1.57,     0.74,     0.97,     0.96,     0.22,     1.85,     0.38,
    "TRT-GPAR",     1.09,     1.28,     1.57,     0.73,     0.96,     0.95,     0.23,     1.87,     0.39
)

# Relevant code
kegg %>% 
    pivot_longer(-NAME, names_to = "ko", values_to = "value") %>% 
    group_by(ko) %>% 
    group_map(~ kruskal.test(value ~ NAME, data = .x))
#> [[1]]
#> 
#>  Kruskal-Wallis rank sum test
#> 
#> data:  value by NAME
#> Kruskal-Wallis chi-squared = 11.27, df = 5, p-value = 0.04628
#> 
#> 
#> [[2]]
#> 
#>  Kruskal-Wallis rank sum test
#> 
#> data:  value by NAME
#> Kruskal-Wallis chi-squared = 10.653, df = 5, p-value = 0.0587
#> 
#> 
#> [[3]]
#> 
#>  Kruskal-Wallis rank sum test
#> 
#> data:  value by NAME
#> Kruskal-Wallis chi-squared = 9.1796, df = 5, p-value = 0.1021
#> 
#> 
#> [[4]]
#> 
#>  Kruskal-Wallis rank sum test
#> 
#> data:  value by NAME
#> Kruskal-Wallis chi-squared = 4.8963, df = 5, p-value = 0.4287
#> 
#> 
#> [[5]]
#> 
#>  Kruskal-Wallis rank sum test
#> 
#> data:  value by NAME
#> Kruskal-Wallis chi-squared = 2.8878, df = 5, p-value = 0.7173
#> 
#> 
#> [[6]]
#> 
#>  Kruskal-Wallis rank sum test
#> 
#> data:  value by NAME
#> Kruskal-Wallis chi-squared = 2.248, df = 5, p-value = 0.8139
#> 
#> 
#> [[7]]
#> 
#>  Kruskal-Wallis rank sum test
#> 
#> data:  value by NAME
#> Kruskal-Wallis chi-squared = 4.4462, df = 5, p-value = 0.4871
#> 
#> 
#> [[8]]
#> 
#>  Kruskal-Wallis rank sum test
#> 
#> data:  value by NAME
#> Kruskal-Wallis chi-squared = 8.208, df = 5, p-value = 0.1451
#> 
#> 
#> [[9]]
#> 
#>  Kruskal-Wallis rank sum test
#> 
#> data:  value by NAME
#> Kruskal-Wallis chi-squared = 10.657, df = 5, p-value = 0.05861

Created on 2022-01-04 by the reprex package (v2.0.1)

1 Like

Thank you very much andresrcs.

This is exactly what i need.

Happy new year 2022!

Again, i am wondering that after kruskal.test, is it possible to conduct post hoc test such as method "BH"?
Thankyou!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.