Comma separator for numbers in R

• Last Updated : 23 May, 2021

In this article, we are going to see the separation of numbers using a comma in R Programming Language. A comma separator in number will give clarity of a number, and we can count easily when a number is separated by a comma.

It can be done with these ways:

1. Using prettyNum()
2. Using format()

Method 1: Using prettyNum()

This function is used to separate the number.

Syntax: prettyNum(input_number, big.mark = “,”, scientific = FALSE)

Parameters:

• input_number – input number
• big mark- it is an separator such that the number should be separated by the given delimiter.
• scientific parameter – It is set to FALSE.

Return: Number separated by comma

Example 1: R program to separate the number with a comma.

R

 # input numberx = 70589999999  # number sepaarted with comma with # prettyNumprettyNum(x, big.mark = ",", scientific = FALSE)

Output:

'70,589,999,999'

Example 2: R program to separate the number with a comma with scientific values

R

 # input numberx = 45000000000000000000  # number sepaarted with comma with# prettyNum scientific = FALSEprettyNum(x, big.mark = ",", scientific = FALSE)  # number sepaarted with comma with# prettyNum with scientific = TRUEprettyNum(x, big.mark = ",", scientific = TRUE)

Output:

'45,000,000,000,000,000,000'
'4.5e+19'

Note: we will not get the comma-separated number when scientific is set to FALSE

Method 2: Using the format()

Used to format the number with a separator

Syntax: format(input_number, big.mark = “,”, scientific = FALSE)

Parameters:

• input_number – input number
• big mark- it is an separator such that the number should be separated by the given delimiter.
• scientific parameter – It is set to FALSE.

Return: Number separated by comma.

Example 1: R program to separate the number with comma

R

 # input numberx = 70589999999  # number sepaarted with comma with  formatprettyNum(x, big.mark = ",", scientific = FALSE)

Output:

'70,589,999,999'

Example 2: R program to separate the number with a comma with scientific values

R

 # input numberx = 720589999999  # number separated with comma with# format scientific = FALSEformat(x, big.mark = ",", scientific = FALSE)  # number separated with comma with# format with scientific = TRUEformat(x, big.mark = ",", scientific = TRUE)

Output:

'720,589,999,999'
'7.2059e+11'

My Personal Notes arrow_drop_up