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

Formatting Numbers and Strings - format() Function in 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.

1. Basic Syntax of format()

The basic syntax of the format() function is:

format(x, ...)

Here, x is the vector of numbers or strings to be formatted.

2. Formatting Numbers

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"

3. Formatting Strings

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"

4. Combining Number and String Formatting:

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"

5. Additional Arguments:

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"

Conclusion:

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.

  1. format() function in R:

    • Description: The 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.
    • Code:
      # Using format() for formatting
      number <- 12345.6789
      formatted_number <- format(number, digits = 2, scientific = FALSE)
      
  2. Formatting numbers in R:

    • Description: Illustrates how to use the format() function to format numeric values, controlling aspects such as decimal places and scientific notation.
    • Code:
      # Formatting numbers in R
      numeric_value <- 123456.789
      formatted_numeric <- format(numeric_value, digits = 3, scientific = FALSE)
      
  3. Custom number formatting in R:

    • Description: Explores custom number formatting using the format() function, allowing users to define their preferred format.
    • Code:
      # Custom number formatting in R
      my_number <- 9876.54321
      custom_format <- format(my_number, big.mark = ",", decimal.mark = "!")
      
  4. Scientific notation in R format():

    • Description: Demonstrates the use of the format() function to represent numbers in scientific notation, controlling the exponent and decimal places.
    • Code:
      # Scientific notation in R format()
      scientific_number <- 0.000012345
      scientific_format <- format(scientific_number, scientific = TRUE, digits = 3)
      
  5. Formatting decimal places in R:

    • Description: Shows how to format numeric values to a specific number of decimal places using the format() function.
    • Code:
      # Formatting decimal places in R
      decimal_value <- 3.141592653589793
      formatted_decimal <- format(decimal_value, digits = 4)
      
  6. Formatting dates with format() in R:

    • Description: Utilizes the format() function to format date objects, specifying the desired date format.
    • Code:
      # Formatting dates with format() in R
      my_date <- Sys.Date()
      formatted_date <- format(my_date, format = "%Y-%m-%d")
      
  7. String formatting with format() in R:

    • Description: Explores the use of the format() function for string formatting, allowing the customization of strings based on specified patterns.
    • Code:
      # String formatting with format() in R
      my_string <- "Hello, World!"
      formatted_string <- format(my_string, width = 15, justify = "right")
      
  8. Using formatC() in R for custom formatting:

    • Description: Introduces the formatC() function for custom formatting, providing more control over the appearance of numeric values.
    • Code:
      # Using formatC() in R for custom formatting
      my_value <- 12345.6789
      custom_formatted_value <- formatC(my_value, format = "f", digits = 2, big.mark = ",", flag = "+")