0

I have a dataframe like below:

dataframe

I want to use Kruskal-Wallis test to compare compound 1 in group A, B, C, D's concentration. For example, compound 1, group A with group B, compare their concentration. The comparison should be made between different group within same compound. Group comparison should be as below.

enter image description here

I tried the code like below

# Performing Kruskal-Wallis test
kruskal.test(Group~Concentration, data = df) )

But it use all Group A data (include compound1, compound2, ...). I want to compare within one compound, how to change the code?

C.YIN
  • 11
  • 5
  • See [Is there a nonparametric equivalent of Tukey HSD?](https://stats.stackexchange.com/questions/17342/is-there-a-nonparametric-equivalent-of-tukey-hsd). – user2974951 Feb 21 '22 at 10:13
  • The internet search term "r Kruskal-Wallis" has loads of results; what have you tried and what are your coding problems: [Helpful guidance for asking questions](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Peter Feb 21 '22 at 10:15
  • @Peter, I added it now, thanks for reminder. – C.YIN Feb 21 '22 at 10:19

1 Answers1

0

Since you didn't provide sample data using dput or similar, I made up a data frame with sample data.

You can run the Kruskal-Wallis-Test in R with the function kruskal.test

set.seed(123)
df <- data.frame(group = sample(x = LETTERS[1:4], size = 20, replace = T),
                 compound = rep(1:4, 5),
                 concentration = rnorm(20, mean = 0.4, sd = 0.2))


kruskal.test(concentration ~ group, data = df)

The result is

> kruskal.test(concentration ~ group, data = df)

    Kruskal-Wallis rank sum test

data:  concentration by group
Kruskal-Wallis chi-squared = 0.5648, df = 3, p-value = 0.9044

Also, check out ?kruskal.test for the function documentation.

tester
  • 1,662
  • 1
  • 10
  • 16
  • Thank you for your answer. Is this code set for certain compound? What confused me most is that I need to set within certain compound, compare groups with concentration. Is this code could solve this ? Thanks a lot in advance. – C.YIN Feb 21 '22 at 10:24
  • All you've shown is how to perform a Kruskal-Wallis test, which is trivial. OP is asking to perform many such tests for each possible combination pair. There are better ways of doing this then manually going through each. – user2974951 Feb 22 '22 at 06:24