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] 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. 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] 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, 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

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

[1] "let_a" "lst_a" "mtrx_a"
After removing last_a variable
Error: object 'lst_a' not found
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

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

 [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

That’s it.

Leave a Comment