Open In App

R Variables - Creating, Naming and Using Variables in R

Last Updated : 06 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

A variable is a memory location reserved for storing data, and the name assigned to it is used to access and manipulate the stored data. The variable name is an identifier for the allocated memory block, which can hold values of various data types during the program’s execution.

In R, variables are dynamically typed, meaning they do not require explicit data type declarations. Instead, a variable in R automatically adopts the data type of the object assigned to it. This flexibility allows variables to change their data type as needed, depending on the values they are assigned.

Creating Variables in R Language

R supports three ways of variable assignment:

  • Using equal operator: operators use an arrow or an equal sign to assign values to variables.
  • Using the leftward operator: data is copied from right to left.
  • Using the rightward operator: data is copied from left to right.

Syntax

Types of Variable Creation in R:

  • Using equal to operators
      variable_name = value
     
  • using leftward operator
     variable_name <- value
     
  • using rightward operator 
     value -> variable_name

Example of Creating Variables in R

Let's look at the live example of creating Variables in R:

R
# using equal to operator
var1 = "hello"
print(var1)

# using leftward operator
var2 <- "hello"
print(var2)

# using rightward operator
"hello" -> var3
print(var3)

Output
[1] "hello"
[1] "hello"
[1] "hello"

Nomenclature of R Variables

When naming variables in R, it’s important to follow these rules:

  1. Valid Characters: A variable name can include letters (a-z, A-Z), numbers (0-9), dots (.), and underscores (_).
    Example: var.1_ is valid.
  2. No Special Characters: Only dots (.) and underscores (_) are allowed. Other special characters like $ or # are not permitted.
    Example: var$1 and var#1 are invalid.
  3. Starting Characters: A variable name can start with a letter or a dot (.).
    Example: .var and var are valid.
  4. Cannot Start with Numbers or Underscore: A variable name cannot begin with a number or an underscore.
    Example: 2var and _var are invalid.
  5. Dot Before Number: If a variable name starts with a dot (.), the character following the dot cannot be a number.
    Example: .3var is invalid.
  6. Avoid Reserved Keywords: A variable name cannot be the same as a reserved keyword in R, such as TRUE, FALSE, NA, etc.
    Example: TRUE and FALSE are not allowed as variable names.

Important Methods for R Variables 

R provides some useful methods to perform operations on variables. These methods are used to determine the data type of the variable, finding a variable, deleting a variable, etc. Following are some of the methods used to work on variables:

1. class() function 

This built-in function is used to determine the data type of the variable provided to it. The R variable to be checked is passed to this as an argument and it prints the data type in return.

Syntax

class(variable)

Example:

R
var1 = "hello"
print(class(var1))

Output
[1] "character"

2. ls() function 

This built-in function is used to know all the present variables in the workspace. This is generally helpful when dealing with a large number of variables at once and helps prevents overwriting any of them.

Syntax:

ls()

Example:

R
# using equal to operator
var1 = "hello"

# using leftward operator
var2 <- "hello"

# using rightward operator
"hello" -> var3

print(ls())

Output
[1] "var1" "var2" "var3"

3. rm() function 

This is again a built-in function used to delete an unwanted variable within your workspace.

This helps clear the memory space allocated to certain variables that are not in use thereby creating more space for others. The name of the variable to be deleted is passed as an argument to it.

Syntax

rm(variable)

Example:

R
"hello" -> var3

# Removing variable
rm(var3)
print(var3)

Output:

Error in print(var3) : object 'var3' not found
Execution halted

Scope of Variables in R programming

The location where we can find a variable and also access it if required is called the scope of a variable. There are mainly two types of variable scopes:

1. Global Variables

Global variables are those variables that exist throughout the execution of a program. It can be changed and accessed from any part of the program.

As the name suggests, Global Variables can be accessed from any part of the program.

  • They are available throughout the lifetime of a program.
  • They are declared anywhere in the program outside all of the functions or blocks.

Global variables are usually declared outside of all of the functions and blocks. They can be accessed from any portion of the program.

R
global = 5

# global variable accessed from within a function
display = function(){
print(global)
}
display()

# changing value of global variable
global = 10
display()

Output
[1] 5
[1] 10

In the above code, the variable 'global' is declared at the top of the program outside all of the functions so it is a global variable and can be accessed or updated from anywhere in the program.

2. Local Variables

Local variables are those variables that exist only within a certain part of a program like a function and are released when the function call ends. Local variables do not exist outside the block in which they are declared, i.e. they can not be accessed or used outside that block.

Local variables are declared inside a block.

R
func = function(){
age = 18    
print(age)
}

cat("Age is:\n")
func()

Output
Age is:
[1] 18

Difference Between Local and Global Variables in R

AspectGlobal VariablesLocal Variables
ScopeDefined outside any function and accessible throughout the program.Defined within a function and accessible only within that function.
LifetimeExists for the duration of the program’s execution or until explicitly deleted.Exists only during the function's execution, and is destroyed once the function finishes.
Naming ConflictsCan cause naming conflicts if used in multiple parts of the program.Restricted to the function where defined, reducing naming conflicts.
Memory UsageRemains in memory throughout the program, potentially using more memory.Created and destroyed when needed, using less memory.

In this article, we’ve covered the basics of variables in R, how to create and use them, and the differences between local and global variables


Next Article
Article Tags :
Practice Tags :

Similar Reads