Currently, I have a data frame with values rounded to 2 decimal places. However, numbers such as 0, 0.5, and 1 are not reported how I need them (0.00, 0.50, 1.00). I'm working in tidyverse, and my current line of code for rounding is mutate_at(vars(-N), funs(round(., 2)))
, which works aside from the problem mentioned above. When I try mutate_at(vars(-N), funs(round(., 2), nsmall = 2)))
I'm given an error.
Asked
Active
Viewed 65 times
0
-
Hi, does [this thread](https://stackoverflow.com/questions/3443687/formatting-decimal-places-in-r) answer your question? – Desmond Jul 21 '22 at 05:42
-
I actually looked at this thread and tried the suggestions before posting this. That's what gave me an error. – William Hutson Jul 21 '22 at 05:46
-
@WilliamHutson Please include sample data and expected output, including any errors you've encoutered; "gave me an error" is not telling us much ;-) – Maurits Evers Jul 21 '22 at 05:53
-
I have a few columns of confidence intervals and other variables in a data frame (e.g., 0.000, 1.000, 0.5678, etc.) and the first block of code in my post will round them to 0, 1, and 0.57 respectively. I need the result to be 0.00, 1.00, and 0.57. Running the second block of code 'mutate_at(vars(-N), funs(round(., 2), nsmall = 2)))' gives the error: Error in call2(): ! Can't create call to non-callable object – William Hutson Jul 21 '22 at 06:05
-
Please provide enough code so others can better understand or reproduce the problem. – Community Jul 21 '22 at 12:19
1 Answers
0
One option is to use num
:
library(tidyverse)
df %>%
mutate(across(-N, num, digits = 2))
Output
a b N
1 0.50 17.52 a
2 1.00 20.58 b
3 2.00 17.47 c
4 7.98 16.56 d
5 5.99 21.11 e
6 7.50 23.48 f
7 4.78 17.11 g
8 9.20 18.77 h
9 4.59 21.39 i
10 6.46 18.89 j
However, it really depends if you need the result in a particular format or if you are just needing to display the result with the 2 decimals. For example, if you don't mind the data being in character, then you can use sprintf
.
df %>%
mutate(across(-N, ~ sprintf("%.2f",round(.x, 2))))
Data
df <- structure(list(a = c(0.5, 1, 2, 7.97630587033927, 5.99083143589087,
7.4982464578934, 4.78181807184592, 9.20062652369961, 4.59063704661094,
6.46314722881652), b = c(17.522836093558, 20.5758226248436, 17.4732198179699,
16.5616007035132, 21.1130455101375, 23.4848396719899, 17.1091119621415,
18.7732864583377, 21.3871927985456, 18.8909927722998), N = c("a",
"b", "c", "d", "e", "f", "g", "h", "i", "j")), class = "data.frame", row.names = c(NA,
-10L))

AndrewGB
- 16,126
- 5
- 18
- 49
-
This seemed to help, thank you! Do you know what I should do for a separate data frame without the column N that has categorical variable columns? Would I try mutate(across(num, digits = 2))? – William Hutson Jul 21 '22 at 06:16
-
@WilliamHutson Are you wanting to apply to only numeric columns or all columns? – AndrewGB Jul 21 '22 at 06:26
-
-
@WilliamHutson You can use `is.numeric`. So, `mutate(across(where(is.numeric), num, digits = 2))` – AndrewGB Jul 21 '22 at 06:34