Variables and Constants in R

In this tutorial, you will learn about constants and variables in R. You will get idea about various types of constants, special constants and built-in constants in R.

Variables and Constants in R

In almost every programming language, variable is a name given to store the data whose value can be changed according to the need of the programmer or user.

Variable Names in R

  • Variable name can be composed of letters, digits, period (.) and underscore (_) character.
  • Variable name starts with letters or period (.) but can not follows by digits.
  • Uppercase and lowercase letters are distinct because R is case-sensitive. That is A and a are different variables in R.
  • Some words are reserved in R for specific functions. Hence reserved words can not be used as variable names.

Valid Variable names in R

x, X, area, stud.height, Test_1, Test_2,.Test1, .Test2 are valid variable names in R.

Note that x and X are different in R, as R is case-sensitive.

In valid Variable names in R

_x, area$1, stud@height, _Test1, .1Test are invalid variable names.

Constants in R

Constants in R

  • The simplest type of data object in R is a constant or scalar.
  • A constant or scalar is an object with one value.
  • In R there are five type of constants namely integer, numeric, logical, complex and string.
  • In addition to these, there are four special type of constants, namely, NULL, NA, Inf and NaN.

Integer Constants

In R an integer constant can be created by using suffix L after the constant. For example to create the integer constant 12, we use 12L.

Examples of integer constants

x <- 12L
x
[1] 12

In the above R code, the first line of code assign the integer constant 12 to the variable x. The symbol <- (combination of two characters < and -) is an assignment operator in R. The second line of code display the value stored in variable x.

The type of storage mode of an object x can be obtained using typeof() function which is integer in this case. And the mode of the object can be determined using mode() function which is numeric in this case.

typeof(x) # type of storage mode of object x
[1] "integer"
mode(x)
[1] "numeric"

In R you can also use hexadecimal representation of integer numbers.

Following R code assign 17 from hexadecimal representation (0x11) to the variable y.

y <- 0x11
y
[1] 17
typeof(y)
[1] "double"
mode(y)
[1] "numeric"

We can also assign large integer with exponent format like 1e4L (which means $1 \times 10^4 = 10000$).

z <- 1e4L
z
[1] 10000
typeof(z)
[1] "integer"

Numeric Constants

Numeric constants consists of an integer part with zero or more digits followed by decimal point (optional) . and a fractional part with zero or more digits (optional) followed by an exponent part consisting of an E or e, an optional sign and an integer constant with zero or more digits. e.g., 2, 35, 0.45, 2e-6, .34, 2.456e+3` are valid numeric constants.

Examples of numeric constants

Below R code assign a numeric constant 0.45 to the variable y.

y <- 0.45
y
[1] 0.45
typeof(y) 
[1] "double"
mode(y)
[1] "numeric"

The double numeric constant 2e-06 means $2\times 10^{-6}=0.000002$. Below R code assign the double numeric constant 2e-06 to the variable z.

z <- 2e-06
z
[1] 2e-06
typeof(z)
[1] "double"
mode(z)
[1] "numeric"

Logical Constants

Logical constants are either TRUE or FALSE. Single character can also be used for logical constants i.e., T or F (no quotes).

Examples of logical constants

Below R code assign the logical value T (TRUE) to the variable Result1.

Result1 <- T
Result1
[1] TRUE

Below R code assign the logical value FALSE to the variable Result2.

Result2 <- FALSE
Result2
[1] FALSE

Complex Constants

Complex constants are similar to the numeric constants but they are followed by i. Only pure imaginary numbers are complex constants. e.g., 1i, 0i, 2.3e-1i are valid complex constants.

Examples of complex constants

# Assign 1i to the variable z1
z1 <- 1i
z1
[1] 0+1i
typeof(z1)
[1] "complex"
mode(z1)
[1] "complex"
# Assign 0i to the variable z2
z2 <- 0i
z2
[1] 0+0i
# Assign 0+0.23i to z3
z3 <- 2.3e-1i
z3
[1] 0+0.23i

String Constants

String constants are delimited by a pair of single (') or double quotes (") and can contain all other printable characters. e.g., Male, Strongly Agree, Pre-test, T are valid string constants.

Examples of String constants

# Assign the string "Male" to gender
gender <- "Male"
gender
[1] "Male"
# Assign the string "Strongly Agree" to Text1
Text1 <- 'Strongly Agree'
Text1
[1] "Strongly Agree"
# Assign the string "Pre-test" to Text2
Text2 <- "Pre-test"
Text2
[1] "Pre-test"
# Assign the string "T" to Text3
Text3 <- "T"
Text3
[1] "T"

Special Constants/Values

In addition to the above constants, there are four special constants in R. They are

  • NULL,
  • Inf or -Inf,
  • NaN and
  • NA.

NULL

The constant NULL is used to indicate an empty object in R.

Examples of NULL

# Assign NULL to the variable A
A <- NULL
# Display the value of variable A
A     
NULL
class(A) 
[1] "NULL"
typeof(A)
[1] "NULL"

Infinity (Inf and -Inf)

If a computation in R results in a number that is too big, R will return positive infinity Inf or negative infinity -Inf depending upon the result. Also a non-zero (positive or negative) number divided by zero results infinity ($\infty$ or $-\infty$). R denotes $-\infty$ by -Inf and $\infty$ by Inf.

Examples of Infinity

Below R code returns the value Inf, as the number $2^{2014}$ is too big.

2 ^ 1024
[1] Inf

Below R code returns the value -Inf, as the number $-2^{2014}$ is too big.

-2 ^ 1024
[1] -Inf

Below R code returns the result Inf as $\dfrac{1}{0}$ is $\infty$.

1 / 0
[1] Inf

Below R code returns the result Inf as $-\dfrac{1}{0}$ is $-\infty$.

-1 / 0
[1] -Inf

Below R code returns the result 0 as $\dfrac{1}{\infty}$ is $0$.

1 / Inf
[1] 0

Not a Number NaN

R supports a special value, called NaN, i.e., Not a Number, which indicates that a numerical result is undefined.

Examples of NaN

0 / 0
[1] NaN
Inf - Inf
[1] NaN
Inf / Inf
[1] NaN

All above R code gives NaN, since the result cannot be defined sensibly.

Not Available NA

R has a particular symbol to indicate the missing value or the value which is not available. R indicate such a value by NA. The result of any arithmetic expression containing NA will produce NA.

Examples of NA

# log is built in function in R
log(NA)  
[1] NA
# This is an arithmetic expression
NA + 10 
[1] NA

Built-in constants in R

R has a small number of built-in constants.

LETTERS

The 26 upper-case letters of Roman alphabet are stored in built-in constant LETTERS.

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

The 26 lower-case letters of Roman alphabet are stored in built-in constant letters.

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

The three-letter abbreviations for English month names are stored in built-in constant month.abb.

month.abb
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

month.name

The English month names (full) are stored in built-in constant month.name.

month.name
 [1] "January"   "February"  "March"     "April"     "May"       "June"     
 [7] "July"      "August"    "September" "October"   "November"  "December" 

pi

The value of $\pi =22/7 = 3.1415927$ is stored as a built-in constant pi.

pi
[1] 3.141593

Endnote

In this tutorial you learned about variables and constants in R.

To learn more about data structures in R, please refer to the following tutorials:

Hopefully you enjoyed learning this tutorial on variables and constants in R.

Leave a Comment