Home > Mobile >  Rounding numbers to not be 1
Rounding numbers to not be 1

Time:12-05

I have a dataset that contains numbers very close to 1, but not actually 1:

library(dplyr)
Data <- tibble(Number = c(0.998971282, 0.97871, 0.98121752874, 0.98921752874, 0.91571358, 1))

It is important that any value that isn't actually 1 is not shown as being 1 in my output i.e. what happens when you use round:

Data %>% mutate(Rounded = round(Number))

Instead an output to two decimal places like this is required:

Data %>% mutate(Desired = c(0.99, 0.98, 0.98, 0.99, 0.92, .00))

CodePudding user response:

Here

Data <- tibble(Number = c(0.998971282, 0.97871, 0.98121752874, 0.98921752874, 0.91571358, 1))
Data %>% mutate(Number=round(Number,2))

Please refer to this video to learn more about the different rounding commands in R https://www.youtube.com/watch?v=kSrnWZcv0tc

CodePudding user response:

Adding to the end of your code %0 you can add the zeros to your 1 and then you can round as you like.

Data <- tibble(Number = c(0.998971282, 0.97871, 0.98121752874, 0.98921752874, 0.91571358, 1)) %0
Data <- Data %>% summarize(Number = round(Number, 2))
output:
Number
1.00
0.98
0.98
0.99
0.92
1.00
  • Related