R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
In R, the format()
function allows for the customization and formatting of numbers and strings. This capability is particularly useful when preparing data for reporting or visualization. In this tutorial, we'll explore how to use the format()
function to format numbers and strings in various ways.
format()
The basic syntax of the format()
function is:
format(x, ...)
Here, x
is the vector of numbers or strings to be formatted.
a) Formatting Decimal Places:
You can specify the number of decimal places using the digits
argument:
num <- 3.14159 formatted_num <- format(num, digits = 3) print(formatted_num) # Outputs: "3.14"
b) Using Scientific Notation:
The scientific
argument controls whether numbers are formatted in scientific notation:
num <- 2000000 formatted_num <- format(num, scientific = TRUE) print(formatted_num) # Outputs: "2e+06"
c) Specifying Width:
The width
argument allows you to set a minimum width for the formatted number. If the number is shorter than this width, it will be right-justified by default:
nums <- c(5, 50, 500) formatted_nums <- format(nums, width = 4) print(formatted_nums) # Outputs: " 5" " 50" " 500"
a) String Width and Justification:
You can set the width of formatted strings and decide their justification using the width
and justify
arguments:
strings <- c("apple", "banana", "cherry") formatted_strings <- format(strings, width = 10, justify = "left") print(formatted_strings)
The justify
argument can take values like "left", "right", "centre", and "none".
b) Trimming White Spaces:
To trim leading and trailing white spaces from strings, use trim
:
str <- " Hello " formatted_str <- format(str, trim = TRUE) print(formatted_str) # Outputs: "Hello"
You can use the format()
function to combine formatting rules for both numbers and strings:
nums <- c(3.14159, 2.71828) formatted_nums <- format(nums, digits = 2, width = 6) print(formatted_nums) # Outputs: " 3.14" " 2.72"
The format()
function has many additional arguments for customization, including:
nsmall
: Minimum number of digits to the right of the decimal point.decimal.mark
: Character to be used for the decimal point.big.mark
: Character to be used between every three integers to the left of the decimal point.num <- 1234567.89 formatted_num <- format(num, big.mark = ",", decimal.mark = ".") print(formatted_num) # Outputs: "1,234,567.89"
The format()
function in R offers a powerful and flexible way to format numbers and strings, making it easier to prepare and present data in a more readable and attractive manner. Whether you're dealing with numbers that need specific decimal precision or strings that need alignment, the format()
function provides the tools necessary to achieve the desired formatting.
format() function in R:
format()
function in R is used for formatting various types of data, including numbers, dates, and strings. It allows customization of the appearance of the data.# Using format() for formatting number <- 12345.6789 formatted_number <- format(number, digits = 2, scientific = FALSE)
Formatting numbers in R:
format()
function to format numeric values, controlling aspects such as decimal places and scientific notation.# Formatting numbers in R numeric_value <- 123456.789 formatted_numeric <- format(numeric_value, digits = 3, scientific = FALSE)
Custom number formatting in R:
format()
function, allowing users to define their preferred format.# Custom number formatting in R my_number <- 9876.54321 custom_format <- format(my_number, big.mark = ",", decimal.mark = "!")
Scientific notation in R format():
format()
function to represent numbers in scientific notation, controlling the exponent and decimal places.# Scientific notation in R format() scientific_number <- 0.000012345 scientific_format <- format(scientific_number, scientific = TRUE, digits = 3)
Formatting decimal places in R:
format()
function.# Formatting decimal places in R decimal_value <- 3.141592653589793 formatted_decimal <- format(decimal_value, digits = 4)
Formatting dates with format() in R:
format()
function to format date objects, specifying the desired date format.# Formatting dates with format() in R my_date <- Sys.Date() formatted_date <- format(my_date, format = "%Y-%m-%d")
String formatting with format() in R:
format()
function for string formatting, allowing the customization of strings based on specified patterns.# String formatting with format() in R my_string <- "Hello, World!" formatted_string <- format(my_string, width = 15, justify = "right")
Using formatC() in R for custom formatting:
formatC()
function for custom formatting, providing more control over the appearance of numeric values.# Using formatC() in R for custom formatting my_value <- 12345.6789 custom_formatted_value <- formatC(my_value, format = "f", digits = 2, big.mark = ",", flag = "+")