How To Get P Value In R

4 min read

Learning how to get a p value in R is one of those small skills that unlocks a surprising amount of statistical work. Now, once you know where R stores that number, you stop copying output from console printouts and start writing scripts that analyze dozens of variables automatically. This guide walks through every common route to a p-value in R — from the built-in test functions like t.test() and chisq.test(), to pulling values out of regression summaries, to computing one by hand from a test statistic using the distribution functions pt(), pnorm(), pchisq(), and pf().

Most guides skip this. Don't And that's really what it comes down to..

What a P-Value Actually Tells You

Before extracting anything, it helps to be clear about the concept. So a p-value is the probability of observing data at least as extreme as what you collected, assuming the null hypothesis is true. It is not the probability that your hypothesis is correct, and it is not a measure of effect size Worth keeping that in mind. Less friction, more output..

That distinction matters practically. In practice, a tiny p-value with a trivial effect is common in large datasets, and a large p-value in a small study often just means the study lacked statistical power. Report the p-value alongside an effect size and a confidence interval, and your analysis becomes far more honest That's the part that actually makes a difference..

Getting a P-Value in R: The Core Idea

R's hypothesis-testing functions return an object of class htest. That object is a list, and the p-value lives inside it under the name p.value.

result <- some_test(x, y)
result$p.value

Run str(result) on any test object and you will see every component available: statistic, parameter, p.value, conf.int, and estimate. Learning this one habit makes almost every future extraction trivial.

Getting P-Values from Built-In Hypothesis Tests

t-Tests

The two-sample and one-sample t-test are the most common entry point.

set.seed(42)
group_a <- rnorm(30, mean = 10, sd = 2)
group_b <- rnorm(30, mean = 11, sd = 2)

test_result <- t.test(group_a, group_b)
test_result$p.value
#> [1] 0.0001374

# One-sample, testing against a known mean
t.test(group_a, mu = 10)$p.value

# Paired design
t.test(before, after, paired = TRUE)$p.value

Two options are worth remembering. var.equal = TRUE switches to the pooled-variance Student t-test, and alternative = "less" or "greater" produces a one-tailed p-value instead of the default two-tailed one.

Correlation Tests

cor_result <- cor.test(mtcars$wt, mtcars$mpg)
cor_result$p.value
cor_result$estimate   # the correlation coefficient itself

By default this runs Pearson's correlation. Add method = "spearman" or method = "kendall" for rank-based alternatives when your data is skewed or contains outliers.

Chi-Square and Fisher's Exact Tests

For categorical data arranged in a contingency table:

tbl <- table(mtcars$am, mtcars$vs)
chisq.test(tbl)$p.value

# When expected cell counts are small (under 5), prefer this
fisher.test(tbl)$p.value

Fisher's exact test is the safer choice for 2×2 tables with sparse cells, because the chi-square approximation becomes unreliable there. R will usually warn you when this is a concern — do not ignore that warning.

ANOVA

model_aov <- aov(mpg ~ factor(cyl), data = mtcars)
summary(model_aov)

The printed output shows Pr(>F). To grab it as a number:

summary(model_aov)[[1]][["Pr(>F)"]][1]

The double-bracket indexing is required because summary() on an aov object returns a list of data frames, one per stratum. That said, the [[1]] selects the first table and the inner [[... ]] selects the column by name The details matter here..

Regression Models

Regression is where most people get stuck, because there is no single p-value — there is one per coefficient plus one for the model overall.

fit <- lm(mpg ~ wt + hp + disp, data = mtcars)
summary(fit)

The fourth column of the coefficient matrix holds the p-values for each predictor:

summary(fit)$coefficients[, 4]

For the overall model fit, use the F-statistic p-value:

pf(summary(fit)$fstatistic[1],
   summary(fit)$fstatistic[2],
   summary(fit)$fstatistic[3],
   lower.tail = FALSE)

For generalized linear models, the identical approach works: summary(glm_fit)$coefficients[, 4]. Note that for glm objects these are **z-based p-values

Dropping Now

Fresh Reads

If You're Into This

More on This Topic

Thank you for reading about How To Get P Value In R. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home