Across +is.numeric + round

Hello everyone.

I want to identify numeric columns and then with across I want to round them to 2.
See my dummy code:

df <- tibble(x = 0.123456789:10.123456789,
             y = 0.123456789:10.123456789,
             z = 0.123456789:10.123456789)
df %>% mutate(across(where(is.numeric(.)), round(.,2)))   ***WRONG***

Thank you for your help.
Cheers

You were close

library(dplyr)

df <- tibble(x = 0.123456789:10.123456789,
             y = 0.123456789:10.123456789,
             z = 0.123456789:10.123456789)
df %>%
    mutate(across(where(is.numeric), round, 2))
#> # A tibble: 11 x 3
#>        x     y     z
#>    <dbl> <dbl> <dbl>
#>  1  0.12  0.12  0.12
#>  2  1.12  1.12  1.12
#>  3  2.12  2.12  2.12
#>  4  3.12  3.12  3.12
#>  5  4.12  4.12  4.12
#>  6  5.12  5.12  5.12
#>  7  6.12  6.12  6.12
#>  8  7.12  7.12  7.12
#>  9  8.12  8.12  8.12
#> 10  9.12  9.12  9.12
#> 11 10.1  10.1  10.1

Created on 2021-02-06 by the reprex package (v1.0.0)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.