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 on 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, [email protected], _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

[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"
[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.

[1] 17
[1] "double"
[1] "numeric"

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

[1] 10000
[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.

[1] 0.45
[1] "double"
[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.

[1] 2e-06
[1] "double"
[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.

[1] TRUE

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


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
[1] 0+1i
[1] "complex"
[1] "complex"
# Assign 0i to the variable z2
## [1] 0+0i
# Assign 0+0.23i to 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
[1] "Male"
# Assign the string "Strongly Agree" to Text1
Text1<-'Strongly Agree'  
[1] "Strongly Agree"
# Assign the string "Pre-test" to Text2
[1] "Pre-test"
# Assign the string "T" to 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.


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

Examples of NULL

# Assign NULL to the variable A
# Display the value of variable A
[1] "NULL"
[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.

[1] Inf

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

[1] -Inf

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

[1] Inf

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

[1] -Inf

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

[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

[1] NaN
Inf - Inf
[1] NaN
[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
[1] NA
# This is an arithmetic expression
[1] NA

Built-in constants in R

R has a small number of built-in constants.


The 26 upper-case letters of Roman alphabet are stored in built-in constant 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"


The 26 lower-case letters of Roman alphabet are stored in built-in constant 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"

The three-letter abbreviations for English month names are stored in built-in constant
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

The English month names (full) are stored in built-in constant
 [1] "January"   "February"  "March"     "April"     "May"       "June"     
 [7] "July"      "August"    "September" "October"   "November"  "December" 


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

[1] 3.141593

I hope you enjoyed this tutorial on variables and constants in R. Hopefully the content is more than sufficient to understand what is variable, how to define variable name and what are different type of constants and built-in constants in R.

VRCBuzz co-founder and passionate about making every day the greatest day of life. Raju is nerd at heart with a background in Statistics. Raju looks after overseeing day to day operations as well as focusing on strategic planning and growth of VRCBuzz products and services. Raju has more than 25 years of experience in Teaching fields. He gain energy by helping people to reach their goal and motivate to align to their passion. Raju holds a Ph.D. degree in Statistics. Raju loves to spend his leisure time on reading and implementing AI and machine learning concepts using statistical models.

Leave a Comment