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:
- Using prettyNum()
- 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.
# input number
x = 70589999999
# number separated with comma with
# prettyNum
prettyNum(x, big.mark = ",", scientific = FALSE)
Output:
'70,589,999,999'
Example 2: R program to separate the number with a comma with scientific values
# input number
x = 45000000000000000000
# number separated with comma with
# prettyNum scientific = FALSE
prettyNum(x, big.mark = ",", scientific = FALSE)
# number separated with comma with
# prettyNum with scientific = TRUE
prettyNum(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
# input number
x = 70589999999
# number separated with comma with format
prettyNum(x, big.mark = ",", scientific = FALSE)
Output:
'70,589,999,999'
Example 2: R program to separate the number with a comma with scientific values
# input number
x = 720589999999
# number separated with comma with
# format scientific = FALSE
format(x, big.mark = ",", scientific = FALSE)
# number separated with comma with
# format with scientific = TRUE
format(x, big.mark = ",", scientific = TRUE)
Output:
'720,589,999,999' '7.2059e+11'