Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a statistical test in R by identifying your outcome, study design, and the quantity you want to estimate or test. In particular, decide whether observations are independent or paired before choosing a function. R’s stats package includes common tests for means, ranks, association, categorical counts, and model comparisons; the function alone does not confirm that its assumptions fit your data.

Start with the question and study design

Before writing R code, specify what the outcome measures and how the observations were collected. Then identify the target: a mean or mean difference, a rank-based comparison, an association, independence between categorical variables, or a term in a fitted model.

  • Independent observations: each observation belongs to one group or condition, without a one-to-one match to an observation in another group.
  • Paired observations: each value has a meaningful match, such as measurements from the same person at two times or deliberately matched subjects.
  • One sample: the question compares a sample with a specified value rather than comparing two groups.

These distinctions determine which test is appropriate. A before-and-after study, for example, is generally not analyzed as if its two sets of measurements came from unrelated people.

Choose a test by outcome and target

Question and data Design R function What it addresses
Is a numeric sample mean different from a specified value? One sample t.test(x, mu = value) A mean relative to the specified value
Do two numeric groups differ in mean? Independent groups t.test(value ~ group) A difference in group means; Welch’s method is the default
Did numeric measurements change within matched units? Paired observations t.test(before, after, paired = TRUE) The mean of the paired differences
Do two samples differ in ranks or distributions? One- or two-sample design, chosen to match the question wilcox.test() A Wilcoxon rank-based test; the two-sample form is also called the Mann–Whitney test
Is there an association between two numeric or ordered variables? Paired measurements on the variables cor.test(x, y, method = "pearson"), "kendall", or "spearman" Product-moment correlation or rank-based association
Do categorical counts fit expected proportions or show independence? Counts from a goodness-of-fit or contingency-table design chisq.test() or fisher.test() Goodness of fit or association/independence in categorical tables
Does a fitted model or model term account for variation? Depends on the fitted model and comparison anova() An analysis-of-variance or deviance table for fitted models

The functions listed are in R’s stats package, documented as containing statistical functions and random-number generation. The R-devel package index retrieved for this article identifies version 4.6.0; documentation pages are for R-devel or patched versions, so confirm defaults against the R version installed on your system: stats package documentation and package index.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Compare means with t.test()

Base R’s t.test() performs one- and two-sample t-tests and supports paired comparisons with paired = TRUE. For two independent samples, var.equal = FALSE is the default: R uses separate variance estimates and Welch’s degrees-of-freedom modification rather than assuming a pooled common variance.

Independent groups

Suppose score is numeric and group identifies two independent groups. A formula call makes the grouping explicit:

t.test(score ~ group, data = dat)

The formula method is intended for a two-group comparison. The default result includes estimated means, an estimated difference, a confidence interval, a test statistic, degrees of freedom, and a p-value. Report the estimated difference and interval alongside the p-value rather than treating a significance threshold as the whole result.

Paired measurements

For matched values stored in corresponding vectors, use:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
t.test(dat$before, dat$after, paired = TRUE)

Pairing must reflect the design: the first and second values on each row or position must belong together. The test evaluates the mean of within-pair differences, not the difference between two unrelated group means.

Check the target and assumptions

A t-test concerns means. Consider whether the sample and design support inference about a mean and whether the observations are independent across units; for paired data, the relevant quantity is the distribution of the differences. t.test() does not automatically diagnose whether the design or assumptions are appropriate. The official reference documents its options and returned estimate and interval: R Core Team’s t.test reference.

Use rank-based tests for an appropriate rank question

wilcox.test() supports one- and two-sample Wilcoxon tests; the two-sample test is also known as Mann–Whitney. Choose the one- or two-sample form according to the study design, including whether measurements are paired. A rank-based test is not automatically a test of medians, nor is it a universal substitute whenever a t-test’s assumptions seem doubtful: its interpretation depends on the distributions and the question being asked.

# Independent two-sample rank-based comparison
wilcox.test(score ~ group, data = dat)

# Paired rank-based comparison
wilcox.test(dat$before, dat$after, paired = TRUE)

Ties and the availability of exact versus approximate p-value calculations can affect the method used. Check the function’s behavior and options for your sample and R version instead of assuming every dataset receives an exact calculation. R’s stats package index also lists kruskal.test() and friedman.test() for other rank-based designs. References: wilcox.test and the stats package index.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Test association with cor.test()

cor.test(x, y) tests association using Pearson’s product-moment correlation by default. You can select Kendall’s tau or Spearman’s rho with the method argument:

cor.test(dat$x, dat$y, method = "pearson")
cor.test(dat$x, dat$y, method = "spearman")
cor.test(dat$x, dat$y, method = "kendall")

Pearson, Spearman, or Kendall?

  • Pearson: tests linear product-moment association. The documented test statistic follows a t distribution with n - 2 degrees of freedom under independent normal sampling.
  • Spearman: assesses rank-based association, useful when the relationship is described in terms of ranks rather than a linear product-moment relationship.
  • Kendall: uses another rank-based measure of association.

The rank-based methods have conditions that affect whether R calculates an exact or approximate p-value. Interpret the coefficient and the method in light of the data; an association test alone does not establish that one variable causes changes in the other. See R Core Team’s cor.test reference.

Analyze categorical counts with chi-squared or Fisher’s exact test

First distinguish a goodness-of-fit question—whether observed counts match specified proportions—from an independence question about two categorical variables in a contingency table. Both use counts, but the null hypothesis and setup differ.

Chi-squared test

chisq.test() can test goodness of fit or independence in a contingency table. For a 2-by-2 table, its correct option applies a continuity correction by default. The function also supports simulated p-values, which may be useful when an asymptotic calculation is not suitable for the table and sampling design.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
# Independence in a contingency table
tab <- table(dat$treatment, dat$outcome)
chisq.test(tab)

Fisher’s exact test

fisher.test() tests independence in contingency tables with fixed marginals. For larger tables, simulation may be reasonable when exact computation is demanding; whether to use it depends on the table and question.

fisher.test(tab)

Inspect the observed counts, expected counts where relevant, and how the observations were sampled before choosing a method. No single cell-count threshold determines the right test for every design. See the official references for chisq.test and fisher.test.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use anova() according to the model question

anova() produces analysis-of-variance or deviance tables for fitted models. The name covers different uses: a table for one fitted model, a comparison of nested models, and a one-way comparison of group means are not interchangeable procedures just because all may be described as ANOVA.

For a multiple-model comparison, confirm that every model was fitted to the same observations. Different missing-value handling can cause models to use different rows, making their comparison invalid. One practical check is to fit the models to a dataset with the required variables and missing rows handled consistently, then compare the fitted objects:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
# Fit both models to the same complete-case data first
models_data <- dat[complete.cases(dat[c("y", "x1", "x2")]), ]
model_small <- lm(y ~ x1, data = models_data)
model_large <- lm(y ~ x1 + x2, data = models_data)
anova(model_small, model_large)

This example illustrates the same-rows requirement for nested model comparison; it does not make every anova() table a test of a simple group-mean difference. See R Core Team’s anova reference.

Read the output as evidence, not a verdict

For each analysis, report the question and design, variables and pairing, selected function and relevant options, and the estimate and uncertainty that answer the question. Include test statistic, degrees of freedom where provided, and p-value, but do not let the p-value stand in for the size or practical meaning of the result. State the scope of the conclusion: a correlation does not establish causality, and an analysis of a sample does not by itself justify claims beyond the population and design it represents.

Official function references describe the result fields and calculation options; consult the documentation matching your installed R release when defaults matter. For broader orientation to R, see An Introduction to R.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.