Skip to main content
v2026.11,858 entries · CC-BY 4.0

Power Analysis in R: pwr Package Functions and Worked Examples

Run power analysis in R with pwr and base R: a design-to-function table, worked sample size calls for t tests, ANOVA, correlation and chi-squared.

Written and maintained by CASRAI Editorial Board

Last updated

Power analysis in R means choosing a sample size, a detectable effect size or a power level by letting a function solve for whichever one you leave out. The most widely used tool is the pwr package, which implements power functions “along the lines of Cohen (1988)”; base R also ships power.t.test and power.prop.test in the stats package. This guide maps each study design to the right function, shows worked calls with the output the package documentation reports, and covers the traps that produce wrong sample sizes: per-group versus total N, one-sided versus two-sided tests, and rounding.

What power analysis in R is used for in research

A power analysis connects four quantities: sample size, effect size, significance level (α) and power. Fix any three and the fourth is determined. In R this is the “leave out the argument you want to calculate” pattern described in the pwr vignette. Researchers use it to justify a sample size in a grant or protocol (see sample size justification for a grant or protocol), to report the smallest effect a fixed sample could detect (see sensitivity power analysis), and to plan a study around an effect size taken from earlier work. For the concepts behind the calculation, start with power analysis and sample size calculation.

Install or access the pwr package

The pwr package is distributed through CRAN, R’s official package repository. Its CRAN page lists version 1.3-0 (published 2020-03-17, GPL 3 or later), imports only stats and graphics, and describes itself as “Basic Functions for Power Analysis”. Install and load it once per session:

install.packages("pwr")
library(pwr)

The functions power.t.test and power.prop.test need no installation because they belong to the stats package that loads with R.

Step-by-step: choosing the function and solving for sample size

Every pwr function takes the same four ingredients: sample size (n, or N and u/v for some tests), an effect size, sig.level and power. Leave out the one you want. The table maps each design to its function and effect-size argument, with the small, medium and large conventions given in the pwr vignette.

Design Function Effect-size argument Small / medium / large
One-sample proportion pwr.p.test h 0.2 / 0.5 / 0.8
Two-sample proportions pwr.2p.test (equal n), pwr.2p2n.test (unequal n) h 0.2 / 0.5 / 0.8
t test (one-sample, two-sample, paired) pwr.t.test (equal n), pwr.t2n.test (unequal n) d 0.2 / 0.5 / 0.8
One-way balanced ANOVA pwr.anova.test f 0.1 / 0.25 / 0.4
Correlation pwr.r.test r 0.1 / 0.3 / 0.5
Chi-squared test pwr.chisq.test w 0.1 / 0.3 / 0.5
General linear model pwr.f2.test f2 0.02 / 0.15 / 0.35

The helpers ES.h, ES.w1, ES.w2 and cohen.ES compute or look up effect sizes. The worked calls below use the examples and outputs from the pwr vignette.

Paired t test

pwr.t.test(d = 0.08/0.25, power = 0.80, sig.level = 0.05,
           type = "paired", alternative = "greater")

The vignette reports n = 61.75209 pairs. Round up to 62 pairs.

t test with a raw difference and standard deviation

Divide the difference you care about by the standard deviation to get d. For a difference of 0.75 and a standard deviation of 2.25, pwr.t.test(d = 0.75/2.25, power = 0.80, sig.level = 0.05) gives n = 142.2462 per group, and base R’s power.t.test(delta = 0.75, sd = 2.25, sig.level = 0.05, power = 0.8) gives 142.2466. Both round up to 143 per group.

One-way ANOVA

pwr.anova.test(k = 3, f = 0.25, sig.level = 0.01, power = 0.9)

This returns n = 94.48714 per group for three groups, so 95 per group.

Correlation

pwr.r.test(r = "medium", power = 0.8)

With r set to the medium convention of 0.3, the vignette reports n = 84.07364, so 85 participants. For a small correlation, pwr.r.test(r = 0.1, sig.level = 0.01, power = 0.8) gives n = 1162.564 for a two-sided test.

Two proportions

pwr.2p.test(h = ES.h(p1 = 0.55, p2 = 0.50), sig.level = 0.05, power = 0.80)

ES.h converts the two proportions to the effect size h. The vignette reports n = 1564.529 per group.

Chi-squared test of association

prob <- matrix(c(0.1, 0.2, 0.4, 0.3), ncol = 2)
pwr.chisq.test(w = ES.w2(prob), power = 0.9, df = 1, sig.level = 0.01)

Here ES.w2(prob) equals 0.2182179 and the result is N = 312.4671. For a goodness-of-fit test, ES.w1(null, alt) gives the effect size from the null and alternative probability vectors; the vignette’s four-category example (w = 0.2886751, df = 3, power 0.8) returns N = 130.8308.

Multiple regression and the general linear model

pwr.f2.test(u = 2, f2 = 0.3/(1 - 0.3), sig.level = 0.001, power = 0.8)

This function returns v, the denominator degrees of freedom, not the sample size. The vignette gives v = 49.88971 and, using n = v + u + 1, arrives at 53 student records. See Cohen’s f-squared for regression for choosing f2.

Solve for power or draw a power curve

To get power instead of n, supply n and omit power. To see how power changes with n, pass a vector and plot the result:

p.out <- pwr.p.test(h = 0.5, n = seq(10, 100, 10), sig.level = 0.05)
plot(p.out)

Output and how to read it

Each function prints a short summary of the test, the argument it solved for, and the arguments you supplied. Read three things:

  1. What n counts. For pwr.t.test (two-sample), pwr.2p.test, pwr.anova.test and power.t.test, n is the number per group (the stats documentation lists n as the number of observations per group). For pwr.chisq.test, the result N is the total. For pwr.f2.test, the result is v, the denominator degrees of freedom.
  2. Round up. The vignette’s own guidance is to round n up, because a fractional value would fall below the target power.
  3. Test direction. A one-sided test needs fewer observations. For r = 0.1, α = 0.01 and power 0.8, alternative = "greater" returns n = 999.2054 against 1162.564 two-sided.

The pwr package and base R can disagree in the later digits. For two proportions of 0.55 and 0.50, pwr.2p.test gives 1564.529 per group and power.prop.test gives 1564.672. Both round up to 1565, so report the rounded value and name the function you used.

Troubleshooting

  • Exactly one argument must be left out. For power.t.test, “exactly one of the parameters n, delta, power, sd, and sig.level must be passed as NULL”. Because sd and sig.level default to non-NULL values, pass sd = NULL or sig.level = NULL explicitly if you want to solve for them.
  • Errors about bracketing the root. The stats documentation says uniroot is used to solve the power equation, so you may see errors from it, notably about inability to bracket the root when invalid arguments are given. Recheck every value you supplied.
  • Sample size looks too small or too large. Check whether n is per group or total, and whether the test is one-sided or two-sided.
  • Unequal group sizes. Use pwr.t2n.test or pwr.2p2n.test rather than the equal-n versions.

Free alternatives

Option What it covers Source
power.t.test, power.prop.test Base R (stats package) power for t tests and two proportions, no installation R documentation (stat.ethz.ch)
pwr package t, proportion, ANOVA, correlation, chi-squared and general linear model power, GPL 3 or later CRAN

For a point-and-click option, see the guide to free statistical software, and for how R compares with commercial packages, see SPSS vs R.

FAQ

Which package should I use for power analysis in R?

Use pwr for the broadest set of common tests (t, ANOVA, correlation, proportions, chi-squared, general linear model). Use base R’s power.t.test or power.prop.test when you want no extra install or need to solve for the standard deviation.

How do I find the sample size for a t test in R?

Call pwr.t.test with d, sig.level and power and leave n out. The result is per group for a two-sample test; round it up.

What effect size should I enter?

Prefer an effect size from prior studies or the smallest difference that matters in your field. The small, medium and large conventions in the table are Cohen’s conventions and serve as a fallback when no better estimate exists. See what is a good sample size for a quantitative study.

Why do pwr.t.test and power.t.test give slightly different answers?

The two functions can differ in the later digits of the result (142.2462 versus 142.2466 in the example above). The rounded sample size is normally the same.

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about Power Analysis in R: pwr Package Functions and Worked Examples

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.