View source: R/data_frame_functions.R
df_round | R Documentation |
Returns a data frame where numeric variables have been rounded to the desired number of digits.
df_round(df, digits = 2, NA_as_empty_string = T, simple = F)
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 |
simple |
(lgl) Whether to just call |
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.
#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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.