Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
fisher.test is the function in R’s stats package that runs Fisher’s exact test on a two-dimensional contingency table of counts. You call it as fisher.test(x) with a matrix (or fisher.test(x, y) with two factors), and it returns an object of class “htest” holding the p-value and, for a 2×2 table, a conditional maximum-likelihood odds-ratio estimate and its confidence interval. The sections below map each argument to the table size it acts on, walk through the documented tea-tasting example, and explain what to do when a large table is slow or fails.
What fisher.test is used for in research
Fisher’s exact test asks whether two categorical variables are associated, using the exact distribution of the table given its row and column totals instead of a large-sample approximation. According to the R documentation, for a 2×2 table the p-value comes from the hypergeometric distribution (the non-central hypergeometric when the odds ratio under test is not 1), and for larger tables it comes from the FEXACT network algorithm of Mehta and Patel (1983). In practice researchers reach for it when cell counts are small and the chi-squared approximation is doubtful; see Fisher’s exact test as a concept for when the test is appropriate and chisq.test in R for the approximate alternative and its small-count warnings.
The test is conditional on the margins. In the two-sided case the documentation states that the p-value is the sum of the probabilities of all tables, with the same margins, whose probability is less than or equal to that of the observed table. That definition is worth keeping in mind when you compare the output with other software, because other tools can define “as extreme” differently.
How to install or access fisher.test in R
fisher.test belongs to the stats package of R itself, so installing R gives you the function with nothing else to add. Install R only from the Comprehensive R Archive Network (CRAN): the Windows download page at cran.r-project.org/bin/windows/base offered R-4.6.2 on 2026-11-16, and the same site carries the macOS and Linux builds. To read the manual page for the function inside R, run ?fisher.test; the same page is published online as the R-devel stats manual entry for fisher.test.
If you want tidy output and post hoc comparisons, the rstatix package wraps the same test. Install it once with install.packages("rstatix") and load it with library(rstatix). Its documentation is at rpkgs.datanovia.com/rstatix.
How to run fisher.test in R step by step
The usage line in the current R-devel documentation is:
fisher.test(x, y = NULL, workspace = 200000, hybrid = FALSE, hybridPars = c(expect = 5, percent = 80, Emin = 1), control = list(), or = 1, alternative = "two.sided", conf.int = TRUE, conf.level = 0.95, simulate.p.value = FALSE, B = 2000)
- Put the counts in a matrix.
xis a two-dimensional contingency table in matrix form, and every entry must be a nonnegative integer. The documentation’s tea-tasting example usesmatrix(c(3, 1, 1, 3), 2). R fills matrices column by column, so this gives the rows (3, 1) and (1, 3). - Or pass two factors. If
xis not a matrix,xandymust be factors (or are coerced to factors), and observations with missing values in either are removed before the table is built.yis ignored whenxis a matrix. - Choose the alternative.
alternativeaccepts “two.sided” (the default), “greater” or “less”, and it acts on 2×2 tables only. For the tea-tasting table the documentation runsfisher.test(TeaTasting, alternative = "greater")because the question is whether the taster identifies the true order more often than chance. - Run the call and keep the result. Assign it, for example
res = fisher.test(matrix(c(3, 1, 1, 3), 2), alternative = "greater"), so thatres$p.value,res$estimateandres$conf.intare available later. - Decide the table-size options.
workspace,hybrid,simulate.p.valueandBonly matter for tables larger than 2×2; the next section lists them.
Arguments by table size
Arguments that act only on 2×2 tables: or (the odds ratio under the null hypothesis, default 1), alternative, conf.int (default TRUE) and conf.level (default 0.95). For larger tables these are not used, which is why larger tables return no odds-ratio estimate or confidence interval.
Arguments that act only on tables larger than 2×2: workspace (the size, in 4-byte units, of the workspace for the network algorithm that computes the non-simulated p-value; a larger workspace can handle bigger problems but may lengthen the run), hybrid (when TRUE, an exact calculation is replaced by a chi-squared approximation wherever the Cochran conditions in hybridPars are met; the defaults are expect = 5, percent = 80, Emin = 1), simulate.p.value (Monte Carlo simulation of the p-value) and B (the number of replicates, default 2000). control is a list passed to the network algorithm.
fisher.test in R for tables larger than 2×2
The documentation’s job-satisfaction example is a 4×4 table whose exact p-value is 0.7827; the same example run with simulate.p.value = TRUE and B = 1e5 gives about 0.78, so the simulated value tracks the exact one closely when the replicate count is large. The documentation also notes that with the default B = 2000 the smallest possible simulated p-value is about 0.0005, so for a result that must be reported to finer precision you raise B. Simulation is conditional on the margins, which must all be strictly positive.
Post hoc tests with rstatix
For a table with two rows and several columns, rstatix provides pairwise_fisher_test(xtab, p.adjust.method = "holm"), which compares columns pairwise and adjusts the p-values; for a table with several rows and two columns it provides row_wise_fisher_test(). The plain fisher_test(xtab) wrapper takes the same table and the arguments workspace, alternative, conf.int, conf.level, simulate.p.value and B, adding detailed = FALSE to control how much it returns.
How to read the fisher.test output
The value is a list of class “htest”. Its components, per the R documentation, are p.value, conf.int (a confidence interval for the odds ratio, 2×2 only), estimate (an estimate of the odds ratio, 2×2 only), null.value (the odds ratio under the null, or), alternative, method (the string “Fisher’s Exact Test for Count Data”) and data.name.
For the one-sided tea-tasting test the documented p-value is 0.2429: with only eight cups in the table, even a result of three correct out of four on each side is not strong evidence against chance. The estimate is not the simple cross-product ratio of the cell counts. R documents it as the conditional maximum-likelihood estimate of the odds ratio, which is why it can differ from the sample odds ratio you would compute by hand. A two-sided p-value should not be assumed to be double the one-sided value; it is computed from the probability-ordering rule described above.
rstatix returns a tibble instead. With the default detailed = FALSE, its documented r×c example has the columns n, p and p.signif; with detailed = TRUE on a 2×2 table the documented output adds estimate, conf.low, conf.high, method and alternative. pairwise_fisher_test() returns group1, group2, n, p, p.adj and p.adj.signif. Its documented job-satisfaction example gives an exact p of 0.783 and a simulated p of 0.784.
Troubleshooting fisher.test in R
The call is slow or fails on a large table
The documentation says the network algorithm can fail when table entries are very large, and that a bigger workspace allows bigger problems at the cost of run time. Work through the options in this order: raise workspace; set hybrid = TRUE so that cells meeting the Cochran conditions use the chi-squared approximation; or set simulate.p.value = TRUE with a large B. The simulation route is the only one that avoids the network algorithm altogether, but it returns an estimate of the p-value, so report the value of B you used.
The table is rejected or the wrong table is built
A matrix must contain nonnegative integers; proportions or percentages are not valid counts. If you pass two vectors, check that they are factors with the levels you intend, because incomplete cases are dropped silently from the table. For a table with an all-zero row or column, simulation requires strictly positive margins, so remove the empty category before using simulate.p.value.
The odds ratio differs from another program
SciPy’s documentation states that the statistic its fisher_exact returns for a 2×2 table is the unconditional maximum-likelihood odds ratio, which differs from the conditional estimate R reports. Compare p-values, not estimates, across the two programs; SciPy’s scipy.stats.contingency.odds_ratio provides the conditional version.
Free alternatives to fisher.test in R
The same exact test is available in the tools in the table below, each described from its own documentation. For the approximate test and the decision between the two, read the chi-square test guide.
| Tool | Call | Table sizes and options documented | What it returns |
|---|---|---|---|
| R, stats package | fisher.test(x) |
2×2 and larger; one- or two-sided on 2×2; simulation and hybrid for larger tables | “htest” object with p-value; odds ratio and interval for 2×2 |
| R, rstatix package | fisher_test(xtab), pairwise_fisher_test(), row_wise_fisher_test() |
Same table and options as base R, plus Holm-adjusted post hoc wrappers | Tibble with n, p and a significance label; p.adj for the pairwise wrapper |
| Python, SciPy | scipy.stats.fisher_exact(table) |
“two-sided” (default), “less” or “greater” on 2×2; a method argument for permutation or Monte Carlo was added in SciPy 1.15.0 |
Statistic (unconditional odds-ratio estimate for 2×2) and p-value |
SciPy’s own example, fisher_exact([[8, 2], [1, 5]]), returns a statistic of 20.0 and a p-value of 0.034965034965034975, which gives you a check value if you port an analysis between languages.
Sources
- R Core Team. Fisher’s Exact Test for Count Data, R-devel stats manual (read 2026-11-16)
- rstatix reference: fisher_test, pairwise_fisher_test, row_wise_fisher_test (read 2026-11-16)
- SciPy documentation: scipy.stats.fisher_exact (read 2026-11-16)
- CRAN: Download R for Windows (read 2026-11-16)
Data: fisher.test arguments by table size, defaults and purpose
Compiled from the R-devel stats manual page for fisher.test: each argument, the table sizes it acts on, its default value and its documented purpose. Arrangement by table size is the editors' own grouping of the documented behaviour.
| Argument | Acts on | Default | Purpose (from the documentation) |
|---|---|---|---|
| x | All tables | none | 2-d contingency table as a matrix of nonnegative integers, or a factor |
| y | All tables (factor input) | NULL | Factor; ignored when x is a matrix |
| alternative | 2x2 only | two.sided | Accepts two.sided, greater or less |
| or | 2x2 only | 1 | Odds ratio under the null hypothesis |
| conf.int | 2x2 only | TRUE | Whether to return a confidence interval for the odds ratio |
| conf.level | 2x2 only | 0.95 | Confidence level of the interval |
| workspace | Larger than 2x2 (exact p) | 200000 | Network-algorithm workspace in 4-byte units; larger allows bigger problems, may run long |
| hybrid | Larger than 2x2 | FALSE | Use chi-squared approximation where the Cochran conditions in hybridPars hold, else exact |
| simulate.p.value | Larger than 2x2 | FALSE | Monte Carlo p-value conditional on margins, which must be strictly positive |
| B | Larger than 2x2 (simulation) | 2000 | Number of Monte Carlo replicates; default gives a minimum p of about 0.0005 |
Sources
- R-devel stats manual: Fisher's Exact Test for Count Data (accessed 2026-11-16)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







