We wish to format the string representation of numbers in a certain way typically for display purposes.
We will cover two common scenarios which are:
This section is concerned with the string representation of numbers. See Numeric for a coverage of the display settings of numbers.
We wish to make sure that numbers are displayed “plainly” without applying scientific notation.
In this example, we wish to create a new column col_2
that is a string representation of the numeric column col_1
while ensuring that the numbers are displayed without scientific notation.
df_2 = df %>%
mutate(col_2 = format(col_1, scientific=FALSE))
Here is how this works:
1000
as 1e+03
and is often triggered automatically when displaying large numbers.format()
while setting the argument scientific
to scientific=FALSE
.paste(col_1, "")
would not work. We may still get scientific notation returned.We wish to add comma separators between each three digits in the string representation of large numbers.
In this example, we wish to create a new column col_2
that is a string representation of the numeric column col_1
with commas added between every three digits.
df_2 = df %>%
mutate(col_2 = format(col_1,
big.mark=',',
scientific=FALSE))
Here is how this works:
1000000
is displayed as 1,000,000
.format()
while setting the argument big.mark
to big.mark=','
.scientific=FALSE
to prevent R from using scientific notation.Extension: No Padding
df_2 = df %>%
mutate(col_2 = format(col_1,
big.mark=',',
scientific=FALSE,
trim=TRUE))
Here is how this works:
trim=TRUE
when calling format()
.We wish to specify the number of decimal places (after the decimal point) to display in a string representation of a floating point number.
In this example, we wish to create a new column col_2
that is a string representation of the numeric column col_1
with two decimal places after the point for each value.
df_2 = df %>%
mutate(col_2 = format(round(col_1, 2), nsmall=2))
Here is how this works:
round(col_1, 2)
, we use the function round()
to round each number in the column col_1
to two decimal places.format()
to convert the rounded numbers to string.nsmall
specifies the minimum number of digits to the right of the decimal point.Extension: String to Numeric to String
df_2 = df %>%
mutate(col_2 = col_1 %>%
parse_number() %>%
round(2) %>%
format(nsmall=2))
Here is how this works:
col_1
is a string, we will first need to convert it to a numeric data type, so we may round it.parse_number()
from the package readr (part of the tidyverse
) to convert a string to a numeric data type. See Numeric.col_1
via %>%
from one function to the next. We think that this is easier to read and understand than three nested function calls.