Rounds all numeric columns of a data frame to a specified number of digits. Non-numeric columns and attributes are preserved.

round_numeric(x, digits)

Arguments

x

A data frame.

digits

Integer. Number of digits to round to.

Value

A data frame with all numeric columns rounded to the specified number of digits.

See also

Examples

data <- data.frame(
  numeric_col1 = c(1.234, 2.345, 3.456),
  numeric_col2 = c(4.567, 5.678, 6.789),
  non_numeric_col = c("A", "B", "C")
)
attr(data, "note") <- "It keeps all the attributes!"
rounded <- round_numeric(data, digits = 2)
rounded
#>   numeric_col1 numeric_col2 non_numeric_col
#> 1         1.23         4.57               A
#> 2         2.35         5.68               B
#> 3         3.46         6.79               C
attr(rounded, "note")
#> [1] "It keeps all the attributes!"