Constants in R

Constants in R

  • R
  • 5 mins read

In this article, we will study the usage of constants in R. 

Variables are the named entities holding values in the memory location. The values of variables are subject to change during the entire course of program execution. However, constants, as the name suggests, have a fixed value throughout the program. Their value is not subject to change ever. Constants are also referred to as scalars in R. 

R also has a set of reserved constants, which we will study, in the later sections of this article. 

Types of Constants in R

There are various types of constants that can majorly be categorized into the following categories; 

Boolean Constants in R

Any boolean entity in R can either be assigned a TRUE or FALSE value. These form the logical or the boolean constants in R. The logical TRUE constant, can also be written as T and the boolean FALSE constant can also be described as F respectively. It can also be recalled that TRUE and FALSE are special reserved keywords in R. 

#assigning the boolean TRUE to the var_true variable
var_true = TRUE
#printing the value of var_true
cat("var_true value : ", var_true)

#assigning the boolean FALSE to the var_false variable
var_false = F
#printing the value of var_false
cat("var_false value : ", var_false)

The code produces the following output : 

var_true value :  TRUE
var_false value :  FALSE

String Constants in R

Variables where the string values are assigned to it, form string constants. String constants can be declared either by using “ “ or ‘ ‘ respectively. They are also known as character constants. 

#assigning the string value to the var_str variable
var_str <- " Learning R at FoxInfoTech"

print("Value of var_str")
print(var_str)

The code prints the following output

[1] "Value of var_str"
[1] " Learning R at FoxInfoTech"

Numeric Constants in R

Numeric Constants can be assigned numeric values, which may belong to integer, float, or complex numbers. Numeric Constants are of the following major types : 

  1. Floating points numbers, for instance, 0.1, 06.4
  2. Integers, for instance, the natural numbers
  3. The numeric constants appended by the letter L are known as Integer constants
  4. Complex numbers are appended by the symbol i. 
  5. The hexadecimal numbers are preceded by the sequence 0X or 0x. 
  6. Exponential numbers, written in the form of 1e-3.

The particular category to which the numeric constant belongs can be validated using the typeof() method. The usage and declaration of the numeric constants are indicated, as follows,

#declaring different types of numeric constants
num1 = 5.040342

num2 = 10L

num3 = 6i

num4 = 0x56

#accessing the type of num1
typeof(num1)
#accessing the type of num2
typeof(num2)
#accessing the type of num3
typeof(num3)
#accessing the type of num4
typeof(num4)

The code produces the following output : 

[1] "double"
[1] "integer"
[1] "complex"
[1] "double"

An interesting thing to notice is that the type of hexadecimal number is also double since it is converted implicitly to its binary equivalent. Also, exponential numbers fall into the category of “double”.

Reserved Constants in R

R has reserved certain keywords in order to depict the value of constants. These are very limited in nature. However, they are extremely useful in the usage of R for the domain of data science. 

Some of the reserved constants are,

NULL

NULL refers to a missing or undefined value. The length of any object assigned to a NULL value is 0. It points to an empty object. NULL is also a keyword in R.

​​#declaring the variable as Null
var = NULL

print("Printing the length of var")
print(length(var))

The code produces the following output : 

[1] "Printing the length of var"
> print(length(var))
[1] 0

NA

NA refers to the non-available value in R. Mathematical operations with NA always yield the result as NA. 

#declaring the variable as NA
var = NA

#adding 10 to var
print("Adding 10 to var")
print(var+10)

The following output is produced : 

[1] "Adding 10 to var"
> print(var+10)
[1] NA

 R’s in-built constants

R contains a few in-built constants to ease up the processing of programs. Some of these constants are indicated in the code below : 

#using R's inbuilt constants

#printing the value of pi 
cat("pi : ",pi)
#printing the value of letters in lower case
cat("letters : ",letters)

#printing the value of letters in upper case
cat("letters : ",LETTERS)

#printing the value of months' abbreviations in upper case
cat("Abbreviations for months : ",month.abb)
-c 

The code produces the following output : 

The code produces the following output : 
pi :  3.141593
letters :  a b c d e f g h i j k l m n o p q r s t u v w x y z
letters :  A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Abbreviations for months :  Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec