Learn R Programming

MOTE (version 1.2.2)

apa: Format numbers for APA-style reporting

Description

Create "pretty" character representations of numeric values with a fixed number of decimal places, optionally keeping or omitting the leading zero for values between -1 and 1.

Usage

apa(value, decimals = 3, leading = TRUE)

Value

A character vector/array (matching the shape of `value`) containing the formatted numbers.

Arguments

value

Numeric input: a single number, vector, matrix, or a data frame with all-numeric columns. Non-numeric inputs will error.

decimals

A single non-negative integer giving the number of decimal places to keep in the output.

leading

Logical: `TRUE` to keep leading zeros on decimals (e.g., `0.25`), `FALSE` to drop them (e.g., `.25`). Default is `TRUE`.

Details

This function formats numbers for inclusion in manuscripts and reports. - When `leading = TRUE`, numbers are rounded and padded to `decimals` places, keeping the leading zero for values with absolute value < 1. - When `leading = FALSE`, the leading zero before the decimal point is removed for values with absolute value < 1. If `value` is a data frame, all columns must be numeric; otherwise an error is thrown.

Examples

Run this code
apa(0.54674, decimals = 3, leading = TRUE)   # "0.547"
apa(c(0.2, 1.2345, -0.04), decimals = 2)     # "0.20" "1.23" "-0.04"
apa(matrix(c(0.12, -0.9, 2.3, 10.5), 2), decimals = 1, leading = FALSE)
# returns a character matrix with ".1", "-.9", "2.3", "10.5"

Run the code above in your browser using DataLab