# R Variables and Constants: The Definitive Guide

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 a dot or underlined 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 a word separator when declaring or defining 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 is not allowed.

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

Period(‘.’) at the beginning of the variable name is 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 invalid.

Reserved words or keywords cannot 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,  you can use the “leftward(<-)”, “rightward(->)”, and “equal to(=)” operators.

``````# 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 2 3 4
 11 21 31 41
 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. Furthermore, R is called a dynamically typed language, meaning we can repeatedly modify the variable’s data type of the same variable 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 2 3 4
 "numeric"
 "double"
------------------------
[]
 11

[]
 21

[]
 31

[]
 41

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

## How to find variables currently in the workspace

To find variables available in the workspace, you can use the “ls()” function. 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

`` "let_a"  "lst_a"  "mtrx_a"``

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

## How to delete variables in R

To remove variables in R, you can use the “rm()” function. The rm() function is used to delete objects from memory. It can delete all objects with the ls() function. The remove() function is similar to the 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

`````` "let_a" "lst_a" "mtrx_a"
After removing last_a variable
Execution halted``````

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

## R Constants

As the name suggests, constants in R are variables whose values 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 integers and those followed by i are regarded as complex.

``````typeof(21)
typeof(21L)
typeof(21i)``````

Output

`````` "double"
 "integer"
 "complex"``````

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

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

Output

`````` 255
 "double"
 36
 "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

`````` "data"
 "character"
 "21"
 "character"
 "2i + j"
 "character"``````

### Inbuilt Constants

R has various built-in 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

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

That’s it.