In this article, you’ll learn about constants in R. And how to use them.
Constants, as the name suggests, are entities whose value cannot be altered. Basic types of constant are numeric constants and character constants.
Numeric Constants
All numbers fall under this category. They can be of type integer
, double
or complex
.
It can be checked with the typeof()
function.
Numeric constants followed by L
are regarded as integer
and those followed by i
are regarded as complex
.
> typeof(101)
[1] "double"
> typeof(101L)
[1] "integer"
> typeof(101i)
[1] "complex"
Also, numeric constants preceded by 0x
or 0X
are interpreted as hexadecimal numbers.
> 0xf
[1] 15
> 0xaf
[1] 175
Character Constants
Character constants can be represented using either single quotes (‘) or double quotes (“) as delimiters.
> 'Hello World'
[1] "Hello World"
> typeof("Hello World")
[1] "character"
Built in Constants
Some of the built-in constants defined in R
Usage
> LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
> letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
> month.abb
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
> month.name
[1] "January" "February" "March" "April" "May" "June"
[7] "July" "August" "September" "October" "November" "December"
> pi
[1] 3.141593
Details
Constant | Description |
LETTERS | the 26 upper-case letters of the Roman alphabet. |
letters | the 26 lower-case letters of the Roman alphabet. |
month.abb | the three-letter abbreviations for the English month names. |
month.name | the English names for the months of the year. |
pi | the ratio of the circumference of a circle to its diameter. |
Note: Since R is a case sensitive language therefore TRUE and True are not the same. Here, TRUE is a reserved word and True can be a variable name.
Conclusion
Hence, we saw what are built in constant in R. And how to use those constant like numeric constant, character constant.
This brings the end of this Blog. We really appreciate your time.
Hope you liked it.
Do visit our page www.zigya.com/blog for more informative blogs on Data Science
Keep Reading! Cheers!
Zigya Academy
BEING RELEVANT