I am racking my brain to understand why round function is not working in a simple data frame. I want to see 2 decimal point after the end of each value in the column numeric_col
while not changing the data type to character
.
This is the data frame I am working on.
df <- structure(list(ttext = c("text1", "text2", "text3", "text4",
"text5", "text6", "text7", "text8", "text9", "text10"), numeric_col = c(1,
2, 3, 4, 5, 6, 7, 8, 9, 10)), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
The code I wrote to see the 2 decimal points is:
library(readxl)
library(dplyr)
library(tidyr)
df <- df %>%
mutate(numeric_col = round(numeric_col,2))
For some reason the data frame doesnt add the 2 decimal points in the numeric column.
attached is the snapshot of both.
You can use the num
function from the pillar/tibble package:
mutate(df, numeric_col=num(numeric_col, digits=2))
# A tibble: 10 × 2
ttext numeric_col
<chr> <num:.2!>
1 text1 1.00
2 text2 2.00
3 text3 3.00
4 text4 4.00
5 text5 5.00
6 text6 6.00
7 text7 7.00
8 text8 8.00
9 text9 9.00
10 text10 10.00
Although the function is described as experimental, you can still perform numeric calculations on these vectors, but possibly at your own risk!