R Tutorial R Charts & Graphs R Statistics R References

R - signif() Function



The R signif() function rounds the values in its first argument to the specified number of significant digits. In special cases it returns the following:

  • If the argument is NaN or infinity, then the result is the same as the argument.

Syntax

signif(x, digits)

Parameters

x Required. Specify column to round off.
digits Optional. Specify number of significant digits to which value has to be round off.

Return Value

Returns the integral value that is nearest to the argument value.

Example: Rounding off the values

In the example below, signif() function is used to round the specified number.

#operating on single element atomic vector
print(signif(-1))
print(signif(0.5))
print(signif(2.0))

cat("\nOperating on vector\n")
#operating on vector
v <- c(10.3, 10.5, 10.75, -10.375, -10.5, -10.7)
print(signif(v))

cat("\nOperating on matrix\n")
#operating on matrix
m <- matrix(c(-10.375, -10.5, -10.7, 100.241, Inf, NaN), nrow=2)
print(signif(m))

cat("\nOperating on first column of matrix\n")
#operating on first column of matrix
print(signif(m[,1]))

The output of the above code will be:

[1] -1
[1] 0.5
[1] 2

Operating on vector
[1]  10.300  10.500  10.750 -10.375 -10.500 -10.700

Operating on matrix
        [,1]    [,2] [,3]
[1,] -10.375 -10.700  Inf
[2,] -10.500 100.241  NaN

Operating on first column of matrix
[1] -10.375 -10.500

Example: Rounding off to certain significant digits

Consider one more example where a numeric vector is rounded off to 3 significant digits.

#creating a vector
v <- c(10.33333, 10.567, 10.7, -10.35, -10.534, 10/3)

#printing the vector after rounding 
#off to 3 significant digits
print(signif(v, 3))

The output of the above code will be:

[1]  10.30  10.60  10.70 -10.40 -10.50   3.33

❮ R Math Functions