I have a dataframe values
as shown below
January February March
0.02345 0.03456 0.04567
0.05432 0.06543 0.07654
I need a command to round each of these values to 3 decimal points. Output should be as shown below
January February March
0.023 0.035 0.046
0.054 0.065 0.077
In case your data frame contains non-numeric characters you may be willing to make use of the function by Jeromy Anglim:
round_df <- function(x, digits) {
# round all numeric variables
# x: data frame
# digits: number of digits to round
numeric_columns <- sapply(x, mode) == 'numeric'
x[numeric_columns] <- round(x[numeric_columns], digits)
x
}
round_df(data, 3)
I think it's neat and quick approach to handle the rounding problem across heterogeneous data frames.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With