Formatting Numbers and Strings in R Programming – format() Function

Formatting Numbers and Strings in detail

The format() function in R is a versatile tool that allows us to convert various objects, such as character strings, date/time data, and numeric values, into specific representations. By providing a wide range of formatting options, the function helps us control the appearance of the output.

Using the format() function, we can customize numerical formatting by specifying parameters such as the number of decimal places (nsmall), the thousands separator (big.mark), the decimal mark (decimal.mark), and other relevant settings. Similarly, it enables the formatting of strings to follow a specific locale or meet particular presentation requirements.

Syntax:

format(x, digits, nsmall, scientific, width, justify = c("left", "right", "center", "none"))

Parameters:

  • x: Input vector.
  • digits: Total number of digits to be displayed.
  • nsmall: Minimum number of digits to the right of the decimal point.
  • scientific: Set to TRUE to use scientific notation.
  • width: Minimum width of the output, with blank spaces padded at the beginning.
  • justify: Align the string output to the left, right, or center.
String Formatting in R

Example: String Placement with format()

# Align string to the left
output1 <- format("Data", width = 10, justify = "l")

# Align string to the center
output2 <- format("Data", width = 10, justify = "c")

# Align string to the right
output3 <- format("Data", width = 10, justify = "r")

# Displaying the results
print(output1)
print(output2)
print(output3)

Output:

[1] "Data      "
[1] "   Data   "
[1] "      Data"
Number Formatting in R

Example 1: Formatting Numbers with format()

# Illustrating the format function

# Rounding off numbers to specific digits
num1 <- format(15.6789012, digits = 5)
num2 <- format(15.6789012, digits = 7)
print(num1)
print(num2)

# Specifying minimum digits after the decimal
num3 <- format(15.6789012, nsmall = 3)
num4 <- format(15.6789012, nsmall = 8)
print(num3)
print(num4)

Output:

[1] "15.679"
[1] "15.67890"
[1] "15.679"
[1] "15.67890120"

Example 2: Scientific Notation and String Representation

# Convert numbers to strings
str1 <- format(6789)
str2 <- format(45.6789)
print(str1)
print(str2)

# Displaying numbers in scientific notation
sci1 <- format(45.6789, scientific = TRUE)
sci2 <- format(45.6789, scientific = FALSE)
print(sci1)
print(sci2)

Output:

[1] "6789"
[1] "45.6789"
[1] "4.56789e+01"
[1] "45.6789"
Date and Time Formatting in R

The format() function can also handle date and time objects, allowing custom string representations to be created.

Example 1: Current Date and Time

# Getting current date and time
current_time <- Sys.time()

# Formatting date-time in "YYYY-MM-DD HH:MM:SS" format
formatted_time <- format(current_time, format = "%Y-%m-%d %H:%M:%S")
print(formatted_time)

Output:

[1] "2025-01-28 15:45:00"

Example 2: Date Formatting with Month Name

# Formatting date with the month's name
specific_date <- as.Date("2024-11-15")
formatted_date <- format(specific_date, format = "%B %d, %Y")
print(formatted_date)

Output:

[1] "November 15, 2024"
  • %Y: Year with century (e.g., 2024).
  • %B: Full month name (e.g., November).
  • %d: Day of the month (e.g., 15).

Comments

Leave a Reply

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