Get Started

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”

View Course

Exercise instructions

The vectors income, percent_change, and p_values are available in your workspace.

  • First, compare the behavior of formatC() to format() by calling formatC() on x with format = "f" and digits = 1. This is the same vector you used with format(), do you see the difference?
  • Call formatC() on y with format = "f" and digits = 1. Notice how digits 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 add flag = "+". This forces the display of the sign.
  • Format p_values using format = "g" and digits = 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
___
Edit and Run Code

This exercise is part of the course

String Manipulation with stringr in R

IntermediateSkill Level
4.4+
9 reviews

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.

Start Learning for Free