log10()
log10(x) The log10() function computes the logarithm of a number in base 10.
Syntax
log10(x)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | numeric | — | A numeric vector or scalar |
Because log10() is vectorized, you can pass a single number or an entire column of values and get back the corresponding logarithms in one call. The examples below illustrate common use cases, from simple scalar inputs to scientific and engineering applications.
Examples
Basic usage
# Log base 10 of 10 is 1
log10(10)
# [1] 1
# Log base 10 of 100 is 2
log10(100)
# [1] 2
# Log base 10 of 1000 is 3
log10(1000)
# [1] 3
# Working with vectors
log10(c(1, 10, 100, 1000))
# [1] 0 1 2 3
The integer results above are a special case — when your input is not a perfect power of ten, log10() returns a decimal value that still conveys the order of magnitude. Scientific notation in R uses exponential form like 1e6 for one million, and log10() recovers the exponent directly, making it a natural companion for numbers written this way.
Scientific notation
# Useful for scientific notation
log10(1e6)
# [1] 6
log10(3.14159)
# [1] 0.4971499
Moving from abstract numbers to real-world science, one of the most familiar applications of base-10 logarithms is the pH scale in chemistry. The pH of a solution is defined as the negative logarithm of its hydrogen ion concentration, so a quick call to -log10() converts a molar concentration directly into a pH value you can interpret.
pH calculation
# Calculate pH from hydrogen ion concentration
h_concentration <- 1e-7
pH <- -log10(h_concentration)
pH
# [1] 7
Beyond one-off calculations, log10() shines as a pattern for extracting the scale of your data. Pairing it with floor() strips away the fractional part and gives you the integer order of magnitude — a quick way to classify values by how many powers of ten they span. This technique is widely used in data exploration to bucket numeric columns before plotting.
Common patterns
Order of magnitude
# Determine order of magnitude
x <- c(0.001, 0.01, 0.1, 1, 10, 100, 1000)
floor(log10(x))
# [1] -3 -2 -1 0 1 2 3
While order-of-magnitude bucketing helps with exploratory analysis, other domains multiply log10() by a constant to create standardized units. Decibel calculations multiply the base-10 log of a power ratio by ten, turning a multiplicative relationship into an additive scale that is easier to reason about and plot. This same pattern — scaling log10() by a domain-specific constant — appears across engineering disciplines.
Decibel calculations
# Power ratio to decibels
power_ratio <- 1000
10 * log10(power_ratio)
# [1] 30
When to use log10 vs log vs log2
log10() is the right choice when your data spans multiple orders of magnitude and you want human-readable scale factors. The result directly tells you the power of ten: log10(1000) = 3 means the value is 10^3.
Use log10() for:
- Plotting data that spans several orders of magnitude (making axis labels readable)
- Converting concentration values to pH or other log-based scientific units
- Decibel calculations in acoustics and electrical engineering
- Any situation where you want to express values as “powers of 10”
Use log() (natural log, base e) when working with statistical models, growth rates, or any mathematical derivation rooted in calculus. Use log2() for binary computing contexts like information entropy.
The functions are related: log10(x) equals log(x) / log(10). R’s general log(x, base = 10) produces the same result as log10(x) but log10() is slightly faster because it is a primitive.
log10() returns -Inf for zero input and NaN for negative inputs. Check your data for these edge cases before computing log10 on a column.
In data visualization, ggplot2 provides scale_x_log10() and scale_y_log10() which apply a log10 transformation to the axis without transforming the underlying data. This is often preferable to transforming the column directly, because the axis labels still show the original values rather than log values.
library(ggplot2)
ggplot(df, aes(x = income, y = spending)) +
geom_point() +
scale_x_log10() +
scale_y_log10()
When your data has true zeros that you cannot log-transform, a common workaround is log10(x + 1) (known as log1p in natural log form). This shifts the domain so that zero maps to zero after transformation. Note that log10(1) = 0, so values near zero still compress into a narrow range. For count data with many zeros, consider whether a log transformation is appropriate at all, or whether a negative-binomial or Poisson model would better represent the distribution.
log10() computes base-10 logarithm directly as a C primitive, faster than log(x, base = 10). It returns NaN for negative inputs and -Inf for zero. Common use cases include pH calculations, decibel conversions, and any domain that works with orders of magnitude. When plotting data spanning many orders of magnitude, log10 scales are conventional: scale_x_log10() in ggplot2 uses base-10.