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

Two Proportion Z-Test: Formula, Worked Example, R and Excel

How to run a two proportion z test (and the one proportion version): formula, worked example with arithmetic, assumptions, and R, Excel and Python code.

Written and maintained by CASRAI Editorial Board

Last updated

A two proportion z test asks whether the share of “successes” in one group differs from the share in another group by more than sampling chance would explain. This page covers the test itself, its one-sample sibling (the one proportion z test), a fully worked example with every step of arithmetic, and the same numbers reproduced in Excel, R and Python.

What is a two proportion z test?

A two proportion z test compares the proportions of an outcome (for example, defective items, responders, or participants who completed a task) in two independent samples. The null hypothesis is that the two population proportions are equal. NIST’s engineering handbook says that when the samples are reasonably large, the normal approximation to the binomial can be used for this comparison (NIST/SEMATECH e-Handbook, section 7.3.3, checked 2026-10-29). The test statistic is a z score, so the reference distribution is the standard normal; see the normal and standard normal distribution for how z maps to tail areas.

One proportion z test

The one proportion z test compares a single sample proportion with a stated value p0, such as a required defect rate. Because p0 is fixed by the null hypothesis, the standard error uses p0 directly and nothing is pooled. NIST gives the statistic and its reject rules in section 7.2.4, “Does the proportion of defectives meet requirements?” (checked 2026-10-29).

Two proportion z test formula

With x1 successes out of n1 in sample 1 and x2 out of n2 in sample 2, the NIST handbook (section 7.3.3) gives the pooled estimate and the statistic as:

p_hat = (x1 + x2) / (n1 + n2)

z = (p1_hat - p2_hat) / sqrt( p_hat * (1 - p_hat) * (1/n1 + 1/n2) )
  • p1_hat, p2_hat: the sample proportions x1/n1 and x2/n2.
  • p_hat: the pooled proportion, the combined successes over the combined sample size. Pooling is used because the null hypothesis says both groups share one true proportion.
  • n1, n2: the two sample sizes.
  • z: the number of standard errors separating the two sample proportions.

For the one proportion version (NIST section 7.2.4): z = (p_hat - p0) / sqrt( p0 * (1 - p0) / N ).

The decision rule follows the alternative hypothesis. For a two-sided test, reject when |z| is at least z1-alpha/2; for H1 that p1 is greater than p2, compare z with z1-alpha; for H1 that p1 is less than p2, compare with zalpha. The choice between one- and two-sided is covered in one-tailed vs two-tailed tests.

Pooled versus unpooled standard error

Question Standard error used Why
One proportion test against p0 sqrt(p0(1 – p0)/N) The null hypothesis fixes the proportion, so no estimate is needed (NIST 7.2.4).
Two proportion test of equality sqrt(p_hat(1 – p_hat)(1/n1 + 1/n2)) with pooled p_hat The null hypothesis says both groups share one proportion (NIST 7.3.3).
Confidence interval for the difference sqrt(p1q1/n1 + p2q2/n2), unpooled An interval does not assume the proportions are equal; this is the textbook form, for example in LibreTexts Statistics for Behavioral Science Majors, section 8.1.

Because the test and the interval use different standard errors, a borderline result can show a p value just above .05 while the interval just excludes zero, or the reverse. The p value versus confidence interval comparison explains how to read the two together.

Worked example with the arithmetic

Suppose 45 of 200 participants in group A and 30 of 200 in group B respond. The test is two-sided at alpha = 0.05. These numbers are made up for illustration.

  1. Sample proportions: p1_hat = 45/200 = 0.225; p2_hat = 30/200 = 0.150. Difference = 0.075.
  2. Pooled proportion: p_hat = (45 + 30)/(200 + 200) = 75/400 = 0.1875.
  3. Standard error: sqrt(0.1875 x 0.8125 x (1/200 + 1/200)) = sqrt(0.15234 x 0.01) = sqrt(0.0015234) = 0.03903.
  4. Test statistic: z = 0.075 / 0.03903 = 1.9215.
  5. Two-sided p value: 2 x P(Z > 1.9215) = 0.0547. One-sided (A greater than B): 0.0273.
  6. Unpooled 95% interval for the difference: standard error sqrt(0.225 x 0.775/200 + 0.15 x 0.85/200) = 0.03885, so 0.075 +/- 1.96 x 0.03885 gives (-0.0011, 0.1511).

The two-sided test does not reject at 0.05 because 1.9215 is below 1.96 and the interval just includes zero. The one-sided test would reject, which is exactly why the direction must be chosen before looking at the data.

One proportion check against NIST

NIST section 7.2.4 works an example with N = 200 items, 26 defective (p_hat = 0.13) against a requirement of p0 = 0.10, one-sided at alpha = 0.05 with z0.95 = 1.645. The standard error is sqrt(0.10 x 0.90/200) = 0.021213, so z = 0.03/0.021213 = 1.414. That is below 1.645, so the null hypothesis is not rejected.

Interpretation

Result Reading
|z| at or above the critical value (1.96 for a two-sided test at 0.05) Reject equal proportions at that alpha level.
|z| below the critical value The data do not provide enough evidence against equal proportions; this is not evidence that the proportions are equal.
Interval for the difference excludes 0 Consistent with a rejection; the interval shows the plausible size of the gap.
Interval includes 0 A difference of zero is among the values compatible with the data.

For how z relates to other test statistics, see the test statistic formula guide, and for when a z test is chosen over a t test, z-test vs t-test.

Assumptions and common mistakes

  • Independent samples and a binary outcome. Each observation is a success or failure and the two groups do not share participants. Paired before-and-after data need a different test.
  • Large enough samples. The handbook’s one proportion test relies on a normal approximation “valid for large N, (N > 30)” and also requires min{Np0, N(1 – p0)} of at least 5 (NIST 7.2.4). NIST does not state a numeric threshold for the two-sample section. A common textbook convention for two groups is at least 10 successes and 10 failures in each (LibreTexts section 8.1). These are different conventions from different sources, so state which one you apply.
  • Choosing the sidedness after seeing the data. Pick the alternative hypothesis first.
  • Using the pooled standard error for the interval. Pool for the test of equality; use the unpooled form for the interval.
  • Very small counts. NIST’s own 2×2 example in section 7.3.3 uses counts of 2, 5, 3 and 2 and applies Fisher’s exact test (p = 0.31060) rather than the z approximation.

How to report it

An example sentence for the worked data: “The response rate did not differ significantly between group A (22.5%, 45/200) and group B (15.0%, 30/200), z = 1.92, p = .055, difference = 7.5 percentage points, 95% CI [-0.1, 15.1].” Report the counts, both proportions, the statistic, the exact p value and the interval, and follow the formatting requirements of your target journal or style guide.

How to calculate it in Excel, R or Python

Excel

With successes in B1 and C1 and sample sizes in B2 and C2:

z (in D1):  =(B1/B2-C1/C2)/SQRT(((B1+C1)/(B2+C2))*(1-(B1+C1)/(B2+C2))*(1/B2+1/C2))
p (in D2):  =2*(1-NORM.S.DIST(ABS(D1),TRUE))

For the data above these return 1.9215 and 0.0547.

R

prop.test(c(45, 30), c(200, 200), correct = FALSE)

The R documentation for prop.test states that with two samples the null hypothesis is that the proportions are equal and the confidence interval is for the difference. The function reports Pearson’s chi-squared statistic. With correct = FALSE this is X-squared = 3.6923 on 1 df (p = 0.0547), which is z squared: 1.9215 squared = 3.6923. The default, correct = TRUE, applies a continuity correction and gives X-squared = 3.2164 (p = 0.0729) with a slightly wider interval, about (-0.0061, 0.1561). binom.test is the exact alternative.

Python

from math import sqrt
from scipy.stats import norm

x1, n1, x2, n2 = 45, 200, 30, 200
p1, p2 = x1 / n1, x2 / n2
p = (x1 + x2) / (n1 + n2)
se = sqrt(p * (1 - p) * (1 / n1 + 1 / n2))
z = (p1 - p2) / se
p_value = 2 * norm.sf(abs(z))
print(round(z, 4), round(p_value, 4))   # 1.9215 0.0547

Frequently asked questions

What is the difference between a one proportion and a two proportion z test?

A one proportion z test compares one sample with a fixed value p0; a two proportion z test compares two independent samples with each other and pools them under the null hypothesis.

Is a two proportion z test the same as a chi-square test?

For a 2×2 table without continuity correction, the chi-squared statistic equals z squared and gives the same two-sided p value, as the worked example shows (3.6923 = 1.9215 squared). See the dictionary entry on the chi-square test.

When should I use Fisher’s exact test instead?

When counts are too small for the normal approximation. NIST section 7.3.3 uses Fisher’s exact test for its small-count example.

Sources

Follow CASRAI

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

Ask CASRAI · free to try

Ask about Two Proportion Z-Test: Formula, Worked Example, R and Excel

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.