round(x, digits=0) signif(x, digits=6)
NA
s) are accepted.
round
, and the total number of digits, in the case of
signif
.
For rounding,
digits
can be negative, for rounding large numbers to the
nearest 10, 100, etc.
x
with data rounded to the specified number of places (
round
),
or with the specified number of significant digits retained (
signif
).
This function will be used as the default method for classes that do not inherit a specific method for the function or for the Math group of functions. The result will retain the class and the attributes. If this behavior is not appropriate, the designer of the class should provide a method for the function or for the Math group.
These functions are in the
Math
group of generic functions -- see
Methods
.
When rounding off a 5, the two common conventions are to: 1) go to the higher
number, or 2) go to the even digit.
The
round
function obeys convention 2, so
round(2.5)
is
2
and
round(3.5)
is
4
.
The rounding mechanism for
signif
is machine dependent, but most machines will
use the "round to even" rule.
Rounding is affected by floating point binary representation. Therefore,
round(1.225,2)
gives the result
1.23
and
round(1.015,2)
gives the result
1.01
.
Use
options(digits=n)
to view more clearly the representation of a
number. This can help determine if rounding will occur in the expected direction for a given number.
round(mydata, dig=2) #round to 2 decimals round(mydata, -1) #round to nearest 10 x <- c(123456, .123456, .000123456) round(x, 3) # produces [1] 123456.000 0.123 0.000 signif(x, 3) # produces [1] 1.23e+05 1.23e-01 1.23e-04 round(c(-1.9, -1.1, 1.1, 1.9)) # produces [1] -2 -1 1 2