R Variables - Creating, Naming and Using Variables in R

Last Updated : 21 Feb, 2026

A variable is a memory location reserved for storing data and the name assigned to it allows you to access and manipulate the stored value. It acts as an identifier for the memory block, which can hold values of different data types during the program’s execution.

  • Variables store values in memory that can be accessed or updated later.
  • R variables are dynamically typed their type is determined by the assigned value.
  • The assignment operator <- is the standard way to assign values, though = can also be used.
r_variables11
R Variable

How to Create Variables in R

In R a variable is created by assigning a value to a name. R supports three ways to assign values to variables:

1. Using the Equal Operator (=)

The equal operator (=) assigns a value directly to a variable.

R
# Assign a string using equal operator
var1 = "Hello Geeks"
print(var1)

Output
[1] "Hello Geeks"

2. Using the Leftward Operator (<-)

The leftward operator <- assigns a value from the right side to the variable on the left. It is widely used in R because it makes the direction of assignment clear and helps distinguish assignment from comparison.

R
# Assign a string using leftward operator
var2 <- "Ready to code"
print(var2)

Output
[1] "Ready to code"

3. Using the Rightward Operator (->)

The rightward operator -> assigns a value from the left side to the variable on the right. It works the same way as <-, but the direction of assignment is reversed.

R
# Assign a string using rightward operator
"Byte-by-Byte" -> var3
print(var3)

Output
[1] "Byte-by-Byte"

Rules for Naming Variables in R

Variable names in R must follow certain rules to work correctly. These rules ensure that the names are valid and do not conflict with R syntax.

1. Valid Characters: Variable names can include letters (a-z, A-Z), numbers (0-9), dots (.) and underscores (_).

R
var1 <- 10
var.name <- "R"
var_name <- TRUE

R is case-sensitive, which means uppercase and lowercase letters are treated as different.

R
value <- 100
Value <- 200

value   
Value   

Output
[1] 100
[1] 200

2. No Special Characters: Special characters like $, #, % or @ are not permitted.

R
var$1 <- 5   
var#1 <- 10 

Output:

Error in parse(text = input): <text>:1:5: unexpected numeric constant

1: var$1

3. Starting Characters: A variable name must start with a letter or a dot (.).

R
var <- 10
.var <- "Hello"

4. Cannot Start with Numbers or Underscore: Variable names cannot begin with a number or an underscore.

R
2var <- 5    
_var <- 10   

Output:

Error in parse(text = input): <text>:1:2: unexpected symbol

1: 2var

5. Dot Before Numbers: If a variable starts with a dot (.), the character immediately after cannot be a number.

R
.3var <- 10 

Output:

Error in parse(text = input): <text>:1:3: unexpected symbol

1: .3var

6. Avoid Reserved Keywords: Reserved keywords in R such as TRUE, FALSE, NA, NULL, Inf, function etc cannot be used as variable names.

R
TRUE <- 1    
function <- 10  

Output:

Error in parse(text = input): <text>:2:10: unexpected assignment

1: TRUE <- 1 # Invalid

2: function <-

Scope of Variables in R programming

Scope of a variable determines where it can be accessed or used in a program. Understanding variable scope helps prevent errors and manage data effectively. There are mainly two types of variable scopes:

1. Global Variables

Global variables are defined outside any function and can be accessed or modified from anywhere in the program. They exist for the entire duration of the program unless explicitly removed.

  • Remain in memory throughout the program which may increase memory usage.
  • Can cause naming conflicts if multiple parts of the program use the same name.
  • Defined outside functions and exist until the program ends or the variable is deleted.
  • They are declared anywhere in the program outside all of the functions or blocks.

Inside the function, global = 20 only changes a new local variable inside the function. The original global variable global outside the function stays the same.

R
global = 5

display = function(){
  global = 20  
  print(global)
}

display()   
print(global)  

Output
[1] 20
[1] 5

2. Local Variables

Local variables are created inside a function or a specific block of code and can only be used within that block. They exist only while the function is running and are removed from memory once the function finishes.

  • Defined inside a function and accessible only within that function.
  • Exist only during the function’s execution and are destroyed after the function ends.
  • Helps avoid conflicts with variables in other parts of the program.
  • Uses memory only when needed and is removed afterward.

Here variable defined inside a function is local and cannot be accessed outside the function.

R
my_function <- function() {
  local_var <- 10   # This is a local variable
  print(local_var)
}

my_function()       
print(local_var)  

Output:

Error: object 'local_var' not found

Important Methods for R Variables

R provides several built-in functions to work with variables. Understanding these functions makes managing variables easier, especially in large programs.

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 <- "HI Geeks 001"
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
var1 <- "hello"
var2 <- 20
var3 <- TRUE

print(ls())

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

3. rm() function

rm() is 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
var3 <- "hello"
# Remove var3
rm(var3)
print(var3)

Output:

Error: object 'var3' not found

4. exists() Function

The exists() function checks whether a variable exists in the workspace. It returns TRUE if the variable exists, otherwise FALSE.

Syntax:

exists("variable_name")

Example:

R
var1 <- 10
print(exists("var1"))  
print(exists("varX"))   

Output
[1] TRUE
[1] FALSE
Comment

Explore