R Variables and Constants: The Definitive Guide

Variables in any programming language are the identifier or the named space in the memory, which are saved and can be referenced and modified later in the program. R is a dynamically typed and interpreted language where type checking of the variable and other objects is done at the run time.

R Variables

Variables in R can store an atomic vector, a group of atomic vectors, or different R Objects. To consider a valid variable name in R, the name consists of letters, numbers, and the dot or underline characters. In the current version of R, there is support for underscore (_) as a valid identifier. Still, it is good practice to use a period(.) as word separators when it comes to declaring or define a variable.

Rules to define a variable in R

The variable name must start with a letter containing a number, letter, underscore(‘_’), and period(‘.’).

Example: varName1, new.variable.name,

Underscore(”) at the beginning of the variable name are not allowed.

Example: ‘_var’ is not a valid variable name.

Period(‘.’) at the beginning of the variable name are allowed but should not be followed by a number. It is preferable in R to use ‘.’, which helps separate the different words for the identifier.

Example: The ‘.var’ is a valid variable name. However, ‘.1var’ is not a valid variable name because the period followed by the number is not valid.

Reserved words or keywords are not allowed to be defined as a variable name in R Language.

Special characters such as ‘#‘, ‘&‘, etc., along with white space (tabs, space) are not allowed in a variable name.

How to assign variables in R

To assign variables in R,  use the leftward(<-), rightward(->), and equal to(=) operator.

# Assignment using equals(=) operator
let_a = c(1, 2, 3, 4)
let_a

# Assignment using leftward(<-) operator
var_a <- c(11, 21, 31, 41)
var_a

# Assignment using rightward(->) operator
c(111, 211, 311, 411) -> const_a
const_a

Output

[1] 1 2 3 4
[1] 11 21 31 41
[1] 111 211 311 411

In the first example, we use an equal operator(=) to assign a value to the variable and print it.

In the second example, we use a leftward operator(<-) to assign a value to the variable and print it.

In the third example, we use a rightward operator(->) to assign a value to the variable and print it.

R variable data type

To check the data type of a variable in R, use the typeof() or class() function. R has different data types like Vector, List, Matrix, Array, and Data Frame. R is called a dynamically typed language, which means that we can modify the variable’s data type of the same variable repeatedly when using it in a program.

let_a <- c(1, 2, 3, 4)
let_a
class(let_a)
typeof(let_a)

cat("------------------------", "\n")

lst_a <- list(11, 21, 31, 41)
lst_a
class(lst_a)
typeof(lst_a)

cat("------------------------", "\n")

mtrx_a <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)
mtrx_a
class(mtrx_a)
typeof(mtrx_a)

Output

[1] 1 2 3 4
[1] "numeric"
[1] "double"
------------------------
[[1]]
[1] 11

[[2]]
[1] 21

[[3]]
[1] 31

[[4]]
[1] 41

[1] "list"
[1] "list"
------------------------
    [,1] [,2] [,3]
[1,]  1    3    5
[2,]  2    4    6
[1] "matrix" "array"
[1] "double"

How to find variables currently in the workspace

To find variables available in the workspace, the ls() function is used. The ls() function can use patterns to match the variable names.

let_a <- c(1, 2, 3, 4)

lst_a <- list(11, 21, 31, 41)

mtrx_a <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)

print(ls())

Output

[1] "let_a"  "lst_a"  "mtrx_a"

The variables starting with dot(.) are hidden, they can be listed using “all.names = TRUE” argument to ls() function.

How to delete variables in R

To remove variables in R, use the rm() function. The rm() function is used to delete objects from memory. It can be used with the ls() function to delete all objects. The remove() function is also similar to rm() function.

let_a <- c(1, 2, 3, 4)

lst_a <- list(11, 21, 31, 41)

mtrx_a <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)

print(ls())
cat("After removing last_a variable", "\n")
rm(lst_a)
lst_a

Output

[1] "let_a" "lst_a" "mtrx_a"
After removing last_a variable
Error: object 'lst_a' not found
Execution halted

From the output, after removing the lst_a variable, it does not find and returns the Error stating the object is not found.

R Constants

Constants in R, as the name suggests, are variables whose value cannot be modified.

There are three types of constants in R.

  1. Numeric Constants
  2. Character Constants
  3. Inbuilt Constants

Numeric Constants

Numeric constants include 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(21)
typeof(21L)
typeof(21i)

Output

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

Numeric constants preceded by 0x or 0X are interpreted as hexadecimal numbers.

0xff
typeof(0xff)
0XF + 21
typeof(0XF + 21)

Output

[1] 255
[1] "double"
[1] 36
[1] "double"

Character Constants

Character constants can be represented using either single quotes (‘) or double quotes (“) as delimiters.

"data"
typeof("data")

"21"
typeof("21")

'2i + j'
typeof('2i + j')

Output

[1] "data"
[1] "character"
[1] "21"
[1] "character"
[1] "2i + j"
[1] "character"

Inbuilt Constants

R has various inbuilt constants along with their values. Let’s see some of that in the following example.

LETTERS
cat("----------------------", "\n")
letters
cat("----------------------", "\n")
month.name
cat("----------------------", "\n")
month.abb
cat("----------------------", "\n")
pi

Output

 [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"
----------------------
 [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"
----------------------
 [1] "January" "February" "March" "April" "May" "June"
 [7] "July" "August" "September" "October" "November" "December"
----------------------
 [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
----------------------
[1] 3.141593

Conclusion

Defining and declaring a variable is an easy task, but you need to follow the rules of the language while doing it. You can change the variable’s value at any given point in time, but you can not change the constants throughout your program. We have seen three ways to assign a value to a variable and learn how to check a variable’s data type. That is it for this tutorial.

 

Leave a Comment