df_round: Round numeric variables of a data frame.

View source: R/data_frame_functions.R

df_roundR Documentation

Round numeric variables of a data frame.

Description

Returns a data frame where numeric variables have been rounded to the desired number of digits.

Usage

df_round(df, digits = 2, NA_as_empty_string = T, simple = F)

Arguments

df

(data frame) A data.frame or matrix.

digits

(int) The number of digits to round to.

NA_as_empty_string

(lgl) Whether to convert NAs to empty strings ("").

simple

(lgl) Whether to just call round on all the numerical columns.

Details

This method is intended for printing purposes. It preserves the number of digits after the decimal point even if they are redundant (e.g. round 2.100 to 2 digits yields 2.10, not 2.1). All columns are converted to character.

Examples

#round to 2 digits by default
iris %>% head %>% df_round
#round to whole numbers
iris %>% head %>% df_round(digits = 0)
#NAs are converted to empty strings
data.frame(a = c(NA, 1.1, -2.2), b = c("abc", "def", NA)) %>% df_round
#but this behavior can be toggled
data.frame(a = c(NA, 1.1, -2.2), b = c("abc", "def", NA)) %>% df_round(NA_as_empty_string = F)
#if for whatever reason you want to just call \code{\link{round}} on all the numerical columns, use \code{simple=T}
iris %>% head %>% df_round(digits = 1, simple = T)

Deleetdk/kirkegaard documentation built on April 22, 2024, 5:22 p.m.