Written and maintained by CASRAI Editorial Board
Published · Last updated
On this page
In R, the p-value is not printed by a single universal function: it is stored in the object a test or model returns. For a test such as t.test() or cor.test() it is the p.value element of the returned “htest” list; for a linear model it is the fourth column of the matrix coef(summary(model)); and for several tests at once, p.adjust() corrects the vector of p-values and format.pval() prepares them for reporting. This guide shows where each p-value lives, how to read it and how to adjust and format it, using the base R documentation.
What it is used for in research
A p-value is the probability, computed under the null hypothesis of the test, of a result at least as extreme as the one observed. If that definition is new, the p-value dictionary entry and what is a p-value cover it, and what a p-value greater than 0.05 means covers the common misreading of non-significant results. In R the practical question is different: given a fitted test or model, which element holds the number, which alternative hypothesis did it test, and has it been adjusted for the number of comparisons made. Each of those three decisions changes the number you report, which is why extracting it with a copied snippet is not enough.
Searchers also write this topic as “p values in r” or “p value in r”; the same functions apply, and the sections below treat the one-test, many-test and model cases in turn.
How to install or access it
R is distributed through CRAN, the Comprehensive R Archive Network, at cran.r-project.org. Every function used on this page, including t.test, cor.test, lm, p.adjust, pairwise.t.test and format.pval, belongs to the packages stats and base, which are part of a standard R installation, so no additional package needs to be installed. The R manual pages cited at the end of this guide are the authority for argument names and return values.
Step-by-step: get the p-value from a test, a model and a set of tests
Step 1. Run the test and keep the object
Assign the result to a name instead of only printing it, so you can query its components:
res = t.test(extra ~ group, data = sleep)res$p.value
According to the t.test manual page, the function returns a list of class “htest” whose components include statistic, parameter (the degrees of freedom), p.value (described as “the p-value for the test”), conf.int, estimate, null.value, stderr, alternative, method and data.name. Running names(res) lists the components of any htest object, and the same $p.value access works across the tests that return this class. For a deeper walk-through of the output of correlation tests see correlation in R.
Step 2. Check which alternative hypothesis was tested
The manual documents alternative = c("two.sided", "less", "greater"), with “two.sided” as the default. A one-sided call changes the p-value, so state it in your methods:
t.test(extra ~ group, data = sleep, alternative = "less")$p.value
Two further defaults matter. In t.test, var.equal = FALSE is the default, so the Welch test is used unless you set it otherwise, and paired = FALSE is the default. For cor.test, the manual says “greater” corresponds to a positive association and “less” to a negative one.
Step 3. Correlation tests, and why the method changes the p-value
ct = cor.test(mtcars$mpg, mtcars$wt, method = "spearman")ct$p.value
The cor.test manual lists method = c("pearson", "kendall", "spearman"). For Pearson’s coefficient the test uses a t distribution with length(x) – 2 degrees of freedom. For Kendall’s tau an exact p-value is computed when there are fewer than 50 finite pairs and no ties (the default exact = NULL decides this). For Spearman’s rho the manual states that algorithm AS 89 is used for n below 1290 with exact = TRUE, and an asymptotic t approximation otherwise. If a data set has ties, R therefore may report an approximate rather than an exact p-value, and the method line of the printed result tells you which was used. The formula interface takes a one-sided formula such as ~ u + v.
Step 4. Regression coefficients and the overall F test
fit = lm(mpg ~ wt, data = mtcars)s = summary(fit)coef(s) # estimate, std. error, t value, p-valuecoef(s)[, 4] # all coefficient p-valuescoef(s)["wt", 4]
The summary.lm manual states that coefficients is a p x 4 matrix with the estimate, its standard error, the t-statistic and the corresponding two-sided p-value, and that coef extracts it. The matrix is not an htest object, so there is no $p.value; you select a column instead. The overall F test is stored differently: summary.lm returns fstatistic as a three-element vector (the value and the numerator and denominator degrees of freedom), not a p-value. The p-value must be computed from those three numbers with the F distribution function pf:
f = s$fstatisticpf(f[1], f[2], f[3], lower.tail = FALSE)
For more on reading the model summary, see summary() in R. If you start from a test statistic rather than a fitted object, converting a z-statistic to a p-value shows the underlying calculation.
Step 5. Adjust for multiple comparisons with p.adjust
When you run many tests, correct the whole vector of p-values together. The manual gives the usage p.adjust(p, method = p.adjust.methods, n = length(p)):
p = c(0.01, 0.02, 0.03, 0.20)p.adjust(p, method = "holm")p.adjust(p, method = "BH")
The manual describes the methods as follows. The method names are “holm”, “hochberg”, “hommel”, “bonferroni”, “BH”, “BY”, “fdr” and “none”. Bonferroni multiplies the p-values by the number of comparisons. The first four (Holm, Hochberg, Hommel and Bonferroni) give strong control of the family-wise error rate, and the manual says there seems no reason to use the unmodified Bonferroni correction because it is dominated by Holm’s method, which is valid under arbitrary assumptions. Hochberg’s and Hommel’s methods are valid when the tests are independent, and Hommel’s is more powerful than Hochberg’s although the difference is usually small. “BH” (also called “fdr”) and “BY” control the false discovery rate; BY is valid under any dependence structure and is more conservative, while BH is typically more powerful. The returned value is a numeric vector of the same length, with names copied. The manual also warns that the argument n should be set only when you know what you are doing, because it changes the number of comparisons assumed.
Step 6. Pairwise tests with adjustment built in
pairwise.t.test(x, g, p.adjust.method = p.adjust.methods, pool.sd = !paired, paired = FALSE, alternative = ...) applies the adjustment for you. Its manual example is pairwise.t.test(Ozone, Month) on the airquality data, and it returns an object of class “pairwise.htest”. Because the default p.adjust.method is the first entry of p.adjust.methods, the default adjustment is Holm.
Step 7. Format tiny p-values for reporting
format.pval(pv, digits = max(1, getOption("digits") - 2), eps = .Machine$double.eps, na.form = "NA") formats a vector of p-values. Values smaller than eps are shown with a less-than sign followed by the threshold, which is how the printed linear-model summary avoids showing a long string of zeros. The manual examples include format.pval(c(0.1, 0.0001, 1e-27)). You can pass your own threshold, for example format.pval(p, eps = 0.001), to print the less-than-0.001 form in a results table, which matches the style most journals request.
Output and how to read it
The data table below summarises where each p-value sits and what the documentation says about it. A few reading rules follow from it.
- Check
res$alternativeandres$methodbefore quoting a p-value: they record whether the test was one- or two-sided and whether, for example, the Welch variant was used. - Coefficient p-values from
summary.lmare two-sided; a one-sided claim on a coefficient needs your own calculation and a stated justification. - An adjusted p-value is not the same quantity as the raw one. Report which method produced it, and the number of comparisons if you set
n. - A p-value does not state the size or importance of an effect. Pair it with the estimate and the confidence interval, which are stored beside
p.valuein an htest object (estimate,conf.int).
Troubleshooting
“$ operator is invalid for atomic vectors” or NULL when taking the p-value
Not every object has a p.value element. A summary.lm object stores its p-values in the coefficients matrix, and its overall F test returns only fstatistic. Use coef(summary(fit))[, 4] for coefficients and pf() for the F test, as above. Use names() or str() on the object to see what it actually contains.
The p-value differs from another package or from a one-sided textbook value
Compare the alternative, var.equal and paired settings, and for correlations the method and whether an exact test was used, since the defaults documented above differ between functions and between other software. In Python, for instance, scipy.stats.ttest_ind defaults to equal_var=True while R’s t.test defaults to the Welch test.
The adjusted p-values are all 1 or unexpectedly large
Check that you passed the complete vector of p-values to p.adjust and did not set n to a larger number than the tests you actually ran; the manual cautions that n should only be set deliberately.
A p-value prints as 0 or in scientific notation
The number is not zero, only smaller than the print precision. Use format.pval() and report it as less than a threshold rather than as zero.
Free alternatives
R is free to use, so the question is mainly which free tool a collaborator already uses. The table lists two Python options that expose the same quantity, with the behaviour stated in their own documentation.
| Tool | Where the p-value is | Documented default or note |
|---|---|---|
R, t.test |
$p.value of the htest result |
Two-sided by default; Welch test by default (var.equal = FALSE) |
Python SciPy, scipy.stats.ttest_ind |
.pvalue attribute of the result |
Two-sided by default; equal_var=True by default, so Welch needs equal_var=False |
Python statsmodels, RegressionResults.pvalues |
.pvalues |
Documented as the two-tailed p-values for the t-statistics of the parameters |
Frequently asked questions
How do I get the p-value from t.test in R?
Save the result and read its p.value element: t.test(x, y)$p.value. The manual describes this component as the p-value for the test, and the default is a two-sided alternative.
How do I get p-values from a linear model in R?
Use coef(summary(fit)) and take the fourth column, which summary.lm documents as the two-sided p-value for each coefficient. The F-test p-value has to be computed from fstatistic with pf.
How do I adjust p values in R for multiple testing?
Pass them to p.adjust(p, method = "holm") for family-wise error control or method = "BH" for false discovery rate control. The manual notes that Holm dominates plain Bonferroni.
How do I show a very small p-value as less than 0.001?
Use format.pval(p, eps = 0.001). Values below eps are printed with a less-than sign and the threshold.
Sources
- R documentation,
t.test, https://stat.ethz.ch/R-manual/R-devel/library/stats/html/t.test.html (read 2026-11-14) - R documentation,
cor.test, https://stat.ethz.ch/R-manual/R-devel/library/stats/html/cor.test.html (read 2026-11-14) - R documentation,
summary.lm, https://stat.ethz.ch/R-manual/R-devel/library/stats/html/summary.lm.html (read 2026-11-14) - R documentation,
p.adjust, https://stat.ethz.ch/R-manual/R-devel/library/stats/html/p.adjust.html (read 2026-11-14) - R documentation,
pairwise.t.test, https://stat.ethz.ch/R-manual/R-devel/library/stats/html/pairwise.t.test.html (read 2026-11-14) - R documentation,
format.pval, https://stat.ethz.ch/R-manual/R-devel/library/base/html/format.pval.html (read 2026-11-14) - CRAN, https://cran.r-project.org/ (read 2026-11-14)
- SciPy documentation,
scipy.stats.ttest_ind, https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.ttest_ind.html (read 2026-11-14) - statsmodels documentation,
RegressionResults.pvalues, https://www.statsmodels.org/stable/generated/statsmodels.regression.linear_model.RegressionResults.pvalues.html (read 2026-11-14)
Data: Where the p-value is stored in common R test and model objects
Compiled from the official R manual pages for t.test, cor.test, summary.lm, pairwise.t.test and p.adjust. For each function it records the returned object type, where the p-value is stored, the default alternative and the adjustment or exact-test rule the manual states.
| Function | Returned object | Where the p-value is | Documented default or rule |
|---|---|---|---|
| t.test | list of class htest | $p.value | alternative = two.sided; var.equal = FALSE (Welch); paired = FALSE |
| cor.test | list of class htest | $p.value | method pearson/kendall/spearman; Kendall exact if fewer than 50 finite pairs and no ties; Spearman AS 89 for n below 1290 with exact = TRUE, otherwise asymptotic t approximation |
| summary.lm | summary object | coefficients matrix, column 4 (two-sided; extract with coef()) | F test stored as fstatistic (value, numdf, dendf), not as a p-value |
| pairwise.t.test | pairwise.htest | Matrix of adjusted p-values for each pair | p.adjust.method defaults to the first entry of p.adjust.methods (holm) |
| p.adjust | numeric vector, names copied | Returned vector is the adjusted p-values | Methods: holm, hochberg, hommel, bonferroni, BH, BY, fdr, none; n = length(p) by default |
Sources
- R manual: t.test (accessed 2026-11-14)
- R manual: cor.test (accessed 2026-11-14)
- R manual: summary.lm (accessed 2026-11-14)
- R manual: pairwise.t.test (accessed 2026-11-14)
- R manual: p.adjust (accessed 2026-11-14)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







