[This article was first published on R on Stats and R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.IntroductionExample 1: two independent groups (PlantGrowth)A small simulation studyThe difference in mediansExample 2: paired data (sleep)When to use permutation tests, and when not toConclusionNote: This post has been written in collaboration with Waqar Ahmad, founder of CalcMega.IntroductionEvery R user meets the same small anxiety early on. You have two groups, you want to know whether they really differ, and the t-test relies on a normal-model reference distribution for small-sample inference. With 200 observations per group, that approximation is often robust. With 10 plants, it deserves scrutiny: a normality test on 10 points has little power to rule out departures, and the central limit theorem is no automatic guarantee.Permutation tests avoid a normal reference distribution, but they do not remove the need for assumptions. They were worked out in the 1930s, in the early days of modern statistics (Fisher’s The Design of Experiments appeared in 1935), and the idea is disarmingly simple: if the treatment did nothing, then the group labels are arbitrary, and shuffling them changes nothing that matters.That last clause is the key assumption, and it has a name: exchangeability. In a randomized experiment, permuting assignments allowed by the design gives an exact test of the sharp null that treatment affects no individual. Without randomization, validity instead requires exchangeability under the null; equality of means alone is not enough when group distributions differ. A permutation test turns exchangeability into a recipe:Compute your test statistic on the data as observed, say, the difference in group means.Shuffle the group labels at random and recompute the statistic.Repeat step 2 many times. The collection of shuffled statistics is the permutation distribution: what your statistic looks like when the labels mean nothing.The p-value is the proportion of shuffles producing a statistic at least as extreme as the one you actually observed.No normal reference distribution is invoked. When the design or exchangeability assumptions hold, this can provide valid inference with small or skewed samples; small sample size does not make arbitrary shuffling safe, and it can limit power and p-value resolution. One clarification before we start, because it is the most common misunderstanding of the method: a permutation p-value is not “the probability that the null hypothesis is true.” It is the proportion of allowed rearrangements that produce a statistic at least as extreme as yours, a statement about how surprising your data would be under the specified null. That is the same careful interpretation a t-test’s p-value demands; permutation tests replace a distributional assumption with design or exchangeability assumptions rather than removing assumptions.In this article we work two complete examples: an independent two-sample test on the PlantGrowth dataset, and a paired test on the sleep dataset, where the shuffling turns into something even neater: flipping signs.Example 1: two independent groups (PlantGrowth)PlantGrowth is one of R’s built-in datasets. Thirty plants were grown under three conditions, a control group and two treatments, and their dried weights recorded. We compare the control group against the first treatment, trt1, ten plants each.The label shuffling below is design-exact only if these group assignments were randomized under the sharp no-effect null. Otherwise, it relies on the groups being exchangeable under the null; the built-in dataset description does not specify the assignment procedure.data(PlantGrowth)ctrl