format Function in R (2 Examples)


This article shows how to encode data objects in a common format using the format() function in the R programming language.

Table of contents:

So now the part you have been waiting for – the examples!

Definition & Basic R Syntax of format Function


Definition: The format R function encodes data objects into common formats.


Basic R Syntax: You can find the basic R programming syntax of the format function below.

format(values)                # Basic R syntax of format function

In the remaining post, I’ll show two examples for the application of the format function in R. So keep on reading…


Creation of Example Data

First, we’ll have to create some data that we can use in the examples below:

set.seed(243266)              # Set seed
x <- rnorm(5)                 # Random values
x                             # Print example vector
# -1.5894026 -0.9007914  0.8997223 -0.4310145  1.5865680

Have a look at the previous output of the RStudio console. It shows that the example data is a numeric vector containing five vector elements.


Example 1: Specify Exact Number of Digits Using format() Function

Example 1 illustrates how to use the format command to define the exact number of digits our vector should show. Have a look at the following R code:

format(x, digits = 3)         # Using digits argument
# "-1.589" "-0.901" " 0.900" "-0.431" " 1.587"

As you can see, the amount of digits was reduced. Note that the format function converts numerics to the character class.


Example 2: Specify Minimum Number of Digits Using format() Function

The following code illustrates how to use the format function to set a minimum number of digits by specifying the nsmall argument. First, let’s set the nsmall argument to 3:

format(x, nsmall = 3)         # Using nsmall argument
# "-1.5894026" "-0.9007914" " 0.8997223" "-0.4310145" " 1.5865680"

The previous R code didn’t change the way how our numbers are shown.

However, if we increase the nsmall argument, the number of shown digits is increased:

format(x, nsmall = 15)        # Larger nsmall
# "-1.589402633173374" "-0.900791358067172" " 0.899722298111224" "-0.431014512150872" " 1.586568038530366"


Video, Further Resources & Summary

In case you need more explanations on the R programming syntax of this tutorial, I can recommend to watch the following video of my YouTube channel. In the video instruction, I explain the R code of this article:



Furthermore, you might want to read some of the related articles on this website.


To summarize: This article showed how to apply the format() function in R. If you have further questions, let me know in the comments.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.