formatC()
The function formatC()
provides an alternative way to format numbers based on C style syntax.
Rather than a scientific
argument, formatC()
has a format
argument that takes a code representing the required format. The most useful are:
"f"
for fixed,"e"
for scientific, and"g"
for fixed unless scientific saves space
When using scientific format, the digits
argument behaves like it does in format()
; it specifies the number of significant digits. However, unlike format()
, when using fixed format, digits
is the number of digits after the decimal point. This is more predictable than format()
, because the number of places after the decimal is fixed regardless of the values being formatted.
formatC()
also formats numbers individually, which means you always get the same output regardless of other numbers in the vector.
The flag
argument allows you to provide some modifiers that, for example, force the display of the sign (flag = "+"
), left align numbers (flag = "-"
) and pad numbers with leading zeros (flag = "0"
). You'll see an example in this exercise.
This is a part of the course
“String Manipulation with stringr in R”
Exercise instructions
The vectors income
, percent_change
, and p_values
are available in your workspace.
- First, compare the behavior of
formatC()
toformat()
by callingformatC()
onx
withformat = "f"
anddigits = 1
. This is the same vector you used withformat()
, do you see the difference? - Call
formatC()
ony
withformat = "f"
anddigits = 1
. Notice howdigits
has consistent behavior regardless of the vector you format. - Format
percent_change
to one decimal place after the decimal point. - Format
percent_change
to one decimal place after the decimal point and addflag = "+"
. This forces the display of the sign. - Format
p_values
usingformat = "g"
anddigits = 2
. This can be useful, since if there are any p-values in scientific notation, they must be < 0.0001.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# From the format() exercise
x <- c(0.0011, 0.011, 1)
y <- c(1.0011, 2.011, 1)
# formatC() on x with format = "f", digits = 1
___
# formatC() on y with format = "f", digits = 1
___
# Format percent_change to one place after the decimal point
___
# percent_change with flag = "+"
___
# Format p_values using format = "g" and digits = 2
___
This exercise is part of the course
String Manipulation with stringr in R
Learn how to pull character strings apart, put them back together and use the stringr package.
You'll start with some basics: how to enter strings in R, how to control how numbers are transformed to strings, and finally how to combine strings together to produce output that combines text and nicely formatted numbers.
Exercise 1: Welcome!Exercise 2: QuotesExercise 3: What you see isn't always what you haveExercise 4: Escape sequencesExercise 5: Turning numbers into stringsExercise 6: Using format() with numbersExercise 7: Controlling other aspects of the stringExercise 8: formatC()Exercise 9: Putting strings togetherExercise 10: Annotation of numbersExercise 11: A very simple tableExercise 12: Let's order pizza!What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.