Open In App

Scope of Variable in R

In R, variables are the containers for storing data values. They are reference, or pointers, to an object in memory which means that whenever a variable is assigned to an instance, it gets mapped to that instance. A variable in R can store a vector, a group of vectors or a combination of many R objects.

Example






# R program to demonstrate
# variable assignment
 
# Assignment using equal operator
var1 = c(0, 1, 2, 3)
print(var1)
 
# Assignment using leftward operator
var2 <- c("Python", "R")
print(var2)
 
# A Vector Assignment
a = c(1, 2, 3, 4)
print(a)
b = c("Debi", "Sandeep", "Subham", "Shiba")
print(b)
 
# A group of vectors Assignment using list
c = list(a, b)
print(c)

Output:

[1] 0 1 2 3
[1] "Python" "R"
[1] 1 2 3 4
[1] "Debi" "Sandeep" "Subham" "Shiba"
[[1]]
[1] 1 2 3 4

[[2]]
[1] "Debi" "Sandeep" "Subham" "Shiba"

Time Complexity: O(1)
Auxiliary Space: O(1)



Naming convention for Variables

Example: 




# R program to demonstrate
# rules for naming the variables
 
# Correct naming
b2 = 7
 
# Correct naming
Amiya_Profession = "Student"
 
# Correct naming
Amiya.Profession = "Student"
 
# Wrong naming
2b = 7
 
# Wrong naming
Amiya@Profession = "Student"

Above code when executed will generate an error because of the wrong naming of variables.

Error: unexpected symbol in "2b"
Execution halted

Scope of a variable

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:

Global Variable

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




# R program to illustrate 
# usage of global variables 
  
# global variable
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

Time Complexity: O(1)
Auxiliary Space: O(1)

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.

Local Variable

Variables defined within a function or block are said to be local to those functions.

Example: 




# R program to illustrate 
# usage of local variables 
 
func = function(){
  # this variable is local to the
  # function func() and cannot be 
  # accessed outside this function
  age = 18   
}
 
print(age)

Time Complexity: O(1)
Auxiliary Space: O(1)

Output:

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

The above program displays an error saying “object ‘age’ not found”. The variable age was declared within the function “func()” so it is local to that function and not visible to the portion of the program outside this function. To correct the above error we have to display the value of variable age from the function “func()” only. Example: 




# R program to illustrate
# usage of local variables
 
func = function(){
# this variable is local to the
# function func() and cannot be
# accessed outside this function
age = 18
print(age)
}
 
cat("Age is:\n")
func()

Output:

Age is:
[1] 18

Time Complexity: O(1)
Auxiliary Space: O(1)

Accessing Global Variables

Global Variables can be accessed from anywhere in the code unlike local variables that have a scope restricted to the block of code in which they are created. Example: 




f = function() {
# a is a local variable here
a <-1
}
f()
 
# Can't access outside the function
print(a) # This'll give error

Output:

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

In the above code, we see that we are unable to access variable “a” outside the function as it’s assigned by an assignment operator(<-) that makes “a” as a local variable. To make assignments to global variables, a super assignment operator(<<-) is used. How super assignment operator works? When using this operator within a function, it searches for the variable in the parent environment frame, if not found it keeps on searching the next level until it reaches the global environment. If the variable is still not found, it is created and assigned at the global level. Example: 




# R program to illustrate
# Scope of variables
 
outer_function = function(){
inner_function = function(){
    # Note that "<<-" operator here
    # makes a as global variable
    a <<- 10
    print(a)
}
inner_function()
print(a)
}
outer_function()
 
# Can access outside the function
print(a)

Output:

[1] 10
[1] 10
[1] 10

When the statement “a <<- 10” is encountered within inner_function(), it looks for the variable “a” in the outer_function() environment. When the search fails, it searches in R_GlobalEnv. Since “a” is not defined in this global environment as well, it is created and assigned there which is now referenced and printed from within inner_function() as well as outer_function().


Article Tags :