round_df: Round Numeric Columns in Data Frame

View source: R/Data_handling.R

round_dfR Documentation

Round Numeric Columns in Data Frame

Description

Round the columns of numeric mode type double to specified (default = 6) significant digits.

Usage

round_df(x, digits = 6)

Arguments

x

A data frame.

digits

An integer. See signif for details.

Details

Actual signif function is used for rounding. Note that other classes might be internally stored as numeric types. Particularly POSIXct class is by default stored as integer (rounding does not apply) but in case of adding (subtracting) double or if displaying fractional seconds, such date-time information will be internally converted to the type double (rounding applies). See examples.

Value

A data frame with varnames and units attributes.

Examples

set.seed(123)
n <- 17520 # number of half-hourly records in one non-leap year
tstamp <- seq(c(ISOdate(2021,3,20)), by = "30 mins", length.out = n)
x <- data.frame(
timestamp = tstamp,
H = rf(n, 1, 2, 1),
LE = rf(n, 1, 2, 1),
qc_flag = sample(c(0:2, NA), n, replace = TRUE)
)
openeddy::varnames(x) <- c("timestamp", "sensible heat", "latent heat",
                           "quality flag")
openeddy::units(x) <- c("-", "W m-2", "W m-2", "-")
str(x)
r <- round_df(x)
head(r)
str(r) # varnames and units are preserved

# Prevent adding double type to POSIXct as it would lead to rounding:
y <- x
y$timestamp <- y$timestamp - 900 # use 900L instead
head(y)
class(y$timestamp)
is.double(y$timestamp)
head(round_df(y))


lsigut/openeddy documentation built on Aug. 5, 2023, 12:25 a.m.