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

Z Test in R: BSDA z.test, DescTools ZTest and pnorm

Run a z test in R with BSDA z.test, DescTools ZTest or pnorm, read the htest output and fix the common argument errors.

Written and maintained by CASRAI Editorial Board

Published

On this page

Base R has no built-in z-test function, so you run a z-test in R with z.test() from the BSDA package, ZTest() from DescTools, or by computing the statistic yourself and passing it to pnorm(). All three need the population standard deviation (σ) to be known, because that is what separates a z-test from a t-test. This guide gives the exact calls for each route, a worked one-sample example you can check by hand, how to read every element of the result, and the mistakes that most often break a z-test in R.

What a z-test in R is used for in research

A z-test compares a sample mean (or a difference of two means) with a hypothesised value when the population standard deviation is known and the sampling distribution of the mean is normal. In research practice that is a narrow situation: measurement instruments with a certified, documented precision, or quality-control settings where σ comes from a long run of earlier data. When σ must be estimated from the sample, the t-test is the right tool. The DescTools manual says so directly: statistical packages often do not include z-test functions because the t-test is usually more appropriate for real-world situations, and its ZTest() is intended for learning z-procedures before moving to t.test(), whose syntax is very similar (DescTools reference manual, p. 659-660). For the decision between the two, see Z-test vs t-test.

What does the z-test tell you?

It tells you how many standard errors the observed mean lies from the value stated in the null hypothesis, and how likely a gap that large or larger would be if the null hypothesis were true. The statistic is z = (x̄ − μ₀) / (σ / √n); the p-value is read from the standard normal distribution. A small p-value is evidence against the null value, not a measure of the size or importance of the difference.

How to install or access z-test functions in R

Both packages are distributed through CRAN, the Comprehensive R Archive Network. The CRAN page for BSDA lists version 1.2.2, published 2023-09-18, depending on R 2.10 or later and importing e1071. The CRAN page for DescTools lists version 0.99.60, published 2025-03-28, depending on R 4.2.0 or later (both pages read 2026-11-20). Installing uses the standard install.packages() function, which downloads and installs packages from CRAN-like repositories (R documentation, utils package).

  1. Install BSDA once: install.packages("BSDA").
  2. Install DescTools once if you want the second route: install.packages("DescTools").
  3. In every new R session, load the package you need: library(BSDA) or library(DescTools).

The manual route needs no package: pnorm() and qnorm() belong to the stats package that ships with R. pnorm(q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) returns the cumulative probability up to q, and qnorm(p) is its inverse (R documentation, The Normal Distribution). Our pnorm in R guide covers lower.tail in more depth.

How to run a one-sample z-test in R step by step

The example uses the eleven measurements printed in the BSDA reference manual’s z.test example, with the known standard deviation of 0.5 used there. The null value of 7 is our own choice for illustration. The numbers below were computed independently with Python’s SciPy and match the formula z = (x̄ − μ₀) / (σ / √n).

  1. Enter the data: x = c(7.8, 6.6, 6.5, 7.4, 7.3, 7.0, 6.4, 7.1, 6.7, 7.6, 6.8). There are n = 11 values and the sample mean is 7.0182.
  2. Run the BSDA test: library(BSDA) then z.test(x, sigma.x = 0.5, mu = 7).
  3. Run the DescTools equivalent: library(DescTools) then ZTest(x, mu = 7, sd_pop = 0.5).
  4. Cross-check by hand with base R:
    z = (mean(x) - 7) / (0.5 / sqrt(length(x)))
    p = 2 * pnorm(-abs(z))

The BSDA manual states: “If y is NULL, a one-sample z-test is carried out with x. If y is not NULL, a standard two-sample z-test is performed.” Its full signature is z.test(x, y = NULL, alternative = "two.sided", mu = 0, sigma.x = NULL, sigma.y = NULL, conf.level = 0.95). The DescTools signature is ZTest(x, y = NULL, alternative = c("two.sided", "less", "greater"), paired = FALSE, mu = 0, sd_pop, conf.level = 0.95).

Worked result for the example

Standard error = 0.5 / √11 = 0.1508. With x̄ = 7.0182 and μ₀ = 7, z = (7.0182 − 7) / 0.1508 = 0.1206. The two-sided p-value is 2 × P(Z > 0.1206) = 0.9040, so these data give no evidence that the mean differs from 7. The 95% confidence interval is 7.0182 ± 1.96 × 0.1508, that is 6.7227 to 7.3137. If you test the one-sided alternative that the mean is less than 7, the p-value is the area below 0.1206 under the standard normal curve = 0.5480.

A two-sample call

The same function handles two samples. The BSDA manual example passes both vectors and both known standard deviations: z.test(x, sigma.x = 0.5, y, sigma.y = 0.5, mu = 2), where y = c(4.5, 5.4, 6.1, 6.1, 5.4, 5.0, 4.1, 5.5) and mu is the hypothesised difference between the means. Computing it by hand: x̄ − ȳ = 7.0182 − 5.2625 = 1.7557, standard error √(0.25/11 + 0.25/8) = 0.2323, z = (1.7557 − 2) / 0.2323 = −1.0516, two-sided p = 0.2930. Our two-sample z-test guide explains the formula and its assumptions.

When you only have summary statistics

BSDA also provides zsum.test(), which takes summarised data (mean, standard deviation, sample size) and whose output is identical to z.test(). Use it when the raw measurements are not available.

How to read the z-test output in R

Both z.test() and ZTest() return an object of class htest, the same structure t.test() returns. According to the BSDA manual its elements are the following, and you can extract any of them with $, for example result$p.value after result = z.test(x, sigma.x = 0.5, mu = 7).

  • statistic: the z value, with its name attribute set to “z”.
  • p.value: the p-value for the chosen alternative.
  • conf.int: the confidence interval at conf.level (default 0.95). For a one-sided alternative one end is infinite, so an upper limit of Inf is not an error.
  • estimate: the sample mean (for two samples, the means of x and y).
  • null.value: the mu you tested.
  • alternative and data.name: the alternative hypothesis and the name of the data object.

The BSDA manual also notes that alternative may be “greater”, “less” or “two.sided” with only the initial letter needed, and that missing values (NAs) and infinite values are removed before the calculation.

What does it mean if my z-score is positive?

A positive z means the sample mean is above the null value μ₀; a negative z means it is below. The sign sets the direction only. With a two-sided test the p-value uses the absolute value of z, so z = +1.96 and z = −1.96 give the same p-value of about 0.05. With a one-sided test the sign matters: a positive z supports alternative = "greater" and works against alternative = "less".

What is the p-value of a z-score?

For a two-sided test it is 2 × P(Z > |z|), which in R is 2 * pnorm(-abs(z)). For a one-sided “greater” test it is pnorm(z, lower.tail = FALSE); for “less” it is pnorm(z). Our z-statistic to p-value guide works through more conversions.

What is the z-score of 95%?

It depends on whether the 95% is split across two tails or sits in one. For a 95% two-sided confidence level or a two-sided α = 0.05, the critical value is qnorm(0.975) = 1.959964, usually rounded to 1.96. For a one-sided α = 0.05 the critical value is qnorm(0.95) = 1.644854, usually rounded to 1.645. These values were computed independently with SciPy’s normal quantile function; they follow from the definition of qnorm() as the inverse of pnorm().

Troubleshooting z-tests in R

R cannot find the function z.test

Base R does not include a z-test function. Install and load a package first (install.packages("BSDA") then library(BSDA)). If more than one loaded package defines a function with the same name, call the one you want explicitly with the double-colon form, BSDA::z.test(...) or DescTools::ZTest(...).

The two packages use different argument names for σ

BSDA names the population standard deviation sigma.x (and sigma.y for a second sample); DescTools names it sd_pop. Copying a call from one package to the other without renaming it will fail or leave the standard deviation unset. Supply σ explicitly: the BSDA signature gives sigma.x a default of NULL, which is not a usable value for the calculation.

You only have the sample standard deviation

If σ is unknown and you plug in the sample standard deviation, you are running an approximate z-test and ignoring the extra uncertainty from estimating σ. The normal reference distribution then understates the uncertainty, most noticeably in small samples. Use t.test() instead; the DescTools manual points to it for exactly this reason. Our t.test in RStudio guide shows the syntax.

A one-sided test shows an infinite confidence limit

That is expected. The BSDA manual describes the confidence interval as half-infinite for one-sided alternatives. If you need a two-sided interval, run the test with alternative = "two.sided" or compute it by hand with mean(x) + c(-1, 1) * qnorm(0.975) * sigma / sqrt(length(x)).

Mixing up paired and two-sample data

DescTools has a paired argument (default FALSE); the BSDA signature has none. For paired measurements, either set paired = TRUE in ZTest() or compute the within-pair differences yourself and run a one-sample test on them.

Free alternatives to a z-test in R

R itself is free, and so are both packages. The table lists other routes that avoid installing anything, or that apply when σ is unknown.

Route When it fits Where to read more
pnorm() and qnorm() by hand σ known; you want to see every step of the calculation, with no extra package pnorm in R (linked above)
t.test() in base R σ unknown and estimated from the sample (the situation DescTools points to t.test() for) t.test in RStudio (linked above)
Normal distribution functions in a spreadsheet You need the same z, critical value or p-value without R Z distribution in Excel
Printed z table Looking up a p-value or critical value from a tabulated z CASRAI guide: how to use a z table

The data table below sets the three R routes side by side, including the argument names that differ between packages.

Frequently asked questions

What does the Z-test tell?

It tells you how far a sample mean lies from a hypothesised value, measured in standard errors, and gives the probability of a gap that large or larger if the null hypothesis were true. It requires a known population standard deviation.

What is the z-score of 95%?

1.96 for a two-sided 95% level (qnorm(0.975) = 1.959964) and 1.645 for a one-sided 95% level (qnorm(0.95) = 1.644854).

What does it mean if my z-score is positive?

The sample mean is above the null value. In a two-sided test the sign does not change the p-value; in a one-sided test it decides which alternative the data support.

What is the p-value of a z-score?

Two-sided: 2 * pnorm(-abs(z)). One-sided upper tail: pnorm(z, lower.tail = FALSE). For z = 1.96 the two-sided value is 0.0500.

Is there a z.test function in base R?

No. Use z.test() from BSDA, ZTest() from DescTools or compute the statistic with pnorm().

Should I use a z-test or a t-test in R?

Use a z-test only when σ is known. Otherwise use t.test().

Sources

Data: R routes for a z-test: function, package version and population-SD argument

Compares the three ways to run a z-test in R, compiled from the CRAN package pages and reference manuals for BSDA and DescTools and from the R stats documentation, checked 2026-11-20. Shows the call, the package version checked, the name of the population standard deviation argument, whether a paired argument exists and the default confidence level.

Period covered: 2023/2025.

R routes for a z-test: function, package version and population-SD argument
RoutePackageVersion checkedPopulation SD argumentHas paired argumentDefault confidence level
BSDA::z.test()BSDA1.2.2sigma.x (and sigma.y for a second sample)No0.95
DescTools::ZTest()DescTools0.99.60sd_popYes (default FALSE)0.95
pnorm() with manual zstats (ships with R)R documentation (R-devel)You supply sigma in your own formulaNot applicableNot applicable (you use qnorm(0.975))

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

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

Ask CASRAI · full access

Ask about Z Test in R: BSDA z.test, DescTools ZTest and pnorm

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

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 open 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 →