Apply round to functions used in summarise_at

2.4k Views Asked by At

I'd like to calculate multiple summary stats for several variables, and would like to round the output values to 2 digits.

Here is a simplified dataset

FA1 = c(0.68, 0.79, 0.65, 0.72, 0.79, 0.78, 0.77, 0.67, 0.77, 0.7)
FA2 = c(0.08, 0.12, 0.07, 0.13, 0.09, 0.12, 0.13, 0.08, 0.17, 0.09)
FA3 = c(0.1, 0.06, 0.08, 0.09, 0.06, 0.08, 0.09, 0.09, 0.06, 0.08)
FA4 = c(0.17, 0.11, 0.19, 0.13, 0.14, 0.14, 0.13, 0.16, 0.11, 0.16)
FA5 = c(2.83, 0.9, 3.87, 1.55, 1.91, 1.46, 1.68, 2.5, 3.0, 1.45)
Species = c("fish", "cat", "dog","fish", "cat", "dog","fish", "cat", "dog","fish")
df = data.frame(FA1, FA2, FA3, FA4, FA5, Species)

I also created a list of the variables that I wanted to calculate summary stats for.

ex.fa = c("FA1", "FA2", "FA3", "FA4", "FA5")

I then used this code to calculate mean and sd

df %>%
group_by(Species) %>%
summarise_at(vars(ex.fa), funs(mean, sd(.,na.rm = TRUE)))

I would like to now round the output to 2 digits, but can't figure out how to incorporate that into my code. Please help - thanks!

0

There are 0 best solutions below