EZ

Eduzan

Learning Hub

Eduzan
Eduzan / R (programming language)

Variables

Computer Science / R (programming language) tutorial chapter - Published 2025-12-13 - R (programming language)

A variable is a memory space allocated to store specific data. The name associated with the variable is used to interact with this reserved memory block. The name given to a variable is known as its variable name. Typically, a variable stores data of a specific data type. The term “variable” reflects its nature—values can vary during the program’s execution.

Variables in R Programming

R is a dynamically typed language. This means variables in R are not explicitly declared with a data type but inherit the data type of the object assigned to them.

This characteristic is also seen in languages like Python and PHP.

Creating Variables in R

R supports three methods for assigning values to variables:

  1. Using the equal operator (=)
    Assigns values to variables directly.
  2. Using the leftward operator (<-)
    Copies data from right to left.
  3. Using the rightward operator (->)
    Copies data from left to right.

Syntax for Creating Variables

Equal Operator:

variable_name = value

Leftward Operator:

variable_name <- value

Rightward Operator:

value -> variable_name

Examples of Creating Variables in R

R Program to Illustrate Variable Creation

# Using equal operator
fruit = "apple"
print(fruit)

# Using leftward operator
city <- "Paris"
print(city)

# Using rightward operator
"blue" -> color
print(color)

Output:

[1] "apple"
[1] "Paris"
[1] "blue"

Nomenclature Rules for R Variables

  1. Variable names can include alphabets, numbers, dot (.), and underscore (_).
    Example: var_1 is valid.
  2. No special characters (except dot and underscore) are allowed.
    Example: var$1 or var#1 is invalid.
  3. Variable names must start with alphabets or a dot (.).
    Example: my_var or .myVar is valid.
  4. Variable names must not begin with numbers or underscores.
    Example: 1var or _var is invalid.
  5. If a variable starts with a dot, the character immediately following cannot be a number.
    Example: .3var is invalid.
  6. Reserved keywords in R (e.g., TRUEFALSE) cannot be used as variable names.

Important Functions for R Variables

1. class() Function: This function identifies the data type of a variable.

Syntax:

class(variable)

Example:

number = 42
print(class(number))

Output:

[1] "numeric"

2. ls() Function: Lists all the variables in the current R workspace.

Syntax:

ls()

Example:

# Assigning values to variables
name = "Alice"
age <- 30
height -> 5.5

print(ls())

Output:

[1] "age" "height" "name"

3. rm() Function: Deletes a specified variable from the workspace.

Syntax:

rm(variable)

Example:

# Assigning and removing a variable
country = "India"
rm(country)
print(country)

Output:

Error in print(country): object 'country' not found

Scope of Variables in R

1. Global Variables: Accessible throughout the program and declared outside of any block or function.

Example:

# Declaring and accessing global variables
global_var = 10

display = function() {
  print(global_var)
}

display()

# Modifying the global variable
global_var = 20
display()

Output:

[1] 10
[1] 20

2. Local Variables: Accessible only within the block or function where they are declared.

Example:

# Declaring and using a local variable
my_function = function() {
  local_var = "This is local"
  print(local_var)
}

cat("Inside the function:\n")
my_function()

cat("Outside the function:\n")
print(local_var)

Output:

Inside the function:
[1] "This is local"
Outside the function:
Error in print(local_var): object 'local_var' not found

Difference Between Local and Global Variables in R

Variables
AspectGlobal VariablesLocal Variables
ScopeAccessible throughout the program.Accessible only within the function/block.
LifetimeExist until the program finishes execution or is removed explicitly.Exist only during the function/block execution.
Naming ConflictsProne to conflicts as accessible globally.Limited scope minimizes conflicts.
Memory UsageOccupies memory throughout the program’s execution.Utilized temporarily, thus conserving memory.
End of lesson.