Rounding and piping

Rate.ILI.Tot = 26.373
I would have thought the two lines of code below would yield identical results, but they do not.
The first returns 26, the second 26.4.
Any idea why?

rate <- update$Rate.ILI.Tot %>% round(,1)
rate <- round(update$Rate.ILI.Tot, 1)

Could you please turn this into a self-contained reprex (short for minimal reproducible example)? It will help us help you if we can be sure we're all working with/looking at the same stuff (libraries loaded, etc.).

Right now the best way to install reprex is:

# install.packages("devtools")
devtools::install_github("tidyverse/reprex")

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

If you run into problems with access to your clipboard, you can specify an outfile for the reprex, and then copy and paste the contents into the forum.

reprex::reprex(input = "fruits_stringdist.R", outfile = "fruits_stringdist.md")

For pointers specific to the community site, check out the reprex FAQ, linked to below.

The reason they are different is because of the comma in your piped version.

library(magrittr)

x <- 26.373

round(x, 1)
#> [1] 26.4

x %>% round(1)
#> [1] 26.4

x %>% round(., 1)
#> [1] 26.4

x %>% round(,1)
#> [1] 26

In the form x %>% round(,1) you are actually not passing 1 to any argument, so the default of 0 is being used.

3 Likes

much appreciated, thanks for helping out a bigginer

1 Like

If your question's been answered, would you mind choosing a solution? (see FAQ below for how) It makes it a bit easier to visually navigate the site and see which questions still need help.

Thanks