How to Apply the format() Function in R (2 Examples)

This article shows how to apply the format() function in R.

Example Data

set.seed(9321646)                 # Creating and printing random vector
my_values <- runif(10)
my_values
# 0.50046976 0.10007698 0.69993540 0.15131349 0.38328304 0.12801148 0.50676644 0.19033864 0.04875698 0.60528036

Example 1: Apply format Function to Define Minimum Number of Digits

format(my_values, nsmall = 13)    # Using nsmall argument
# "0.5004697602708" "0.1000769843813" "0.6999353955034" "0.1513134876732" "0.3832830404863" "0.1280114827678" "0.5067664431408" "0.1903386432678" "0.0487569770776" "0.6052803569473"

Example 2: Apply format Function to Define Exact Number of Digits

format(my_values, digits = 1)     # Using digits argument
# "0.50" "0.10" "0.70" "0.15" "0.38" "0.13" "0.51" "0.19" "0.05" "0.61"

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.
You need to agree with the terms to proceed

Menu
Top