Difference of Global & Local Variables in R (2 Examples)

 

This page shows how to use global and local variables in the R programming language.

Table of contents:

Here’s how to do it!

 

Example 1: Create Function that Stores Output in Local Environment

This example shows how to create a local data object within a user-defined function in R.

First, let’s create our own function:

my_fun1 <- function() {        # Create user-defined function with <-
  out1 <- 10
}

As you can see, within our function we are storing the value 10 in the variable out1.

Next, let’s run this function:

my_fun1()                      # Applying user-defined function

The variable out1 was defined using a single arrow (i.e. “<-“) as assignment operator.

Let’s try to print our data object out1 to the RStudio console:

out1                           # Trying to print output
# Error: object 'out1' not found

The RStudio console returns an error message that our object out1 is not found.

Let’s see if it even exists:

exists("out1")                 # Doesn't exist in global environment
# FALSE

The exists function returns the logical value FALSE.

In other words: the variable out1 is not defined in the global environment or in the scope that we are currently using.

What can we do about this? That’s what I’ll explain next!

 

Example 2: Create Function that Stores Output in Global Environment

This example illustrates how to save variables within a function in the global environment.

Let’s create another user-defined function:

my_fun2 <- function() {        # Create user-defined function with <<-
  out2 <<- 10
}

Have a close look at the previous R code. As you can see, we have used a double arrow as assignment operator (i.e. “<<-“).

Let’s apply our function…

my_fun2()                      # Applying user-defined function

…and let’s check whether the output variable out2 exists:

out2                           # Printing output to console
# 10

The data object out2 exists and contains the value 10.

In other words: The double arrow assigned the output of our user-defined function to the global environment instead of the local environment.

 

Video & Further Resources

If you need more explanations on the topics of this article, you may watch the following video of my YouTube channel. In the video instruction, I explain the content of this article in the R programming language.

 

The YouTube video will be added soon.

 

Furthermore, you may have a look at some of the related tutorials on my homepage:

 

In this article you learned how to define the scope of data objects in R. Tell me about it in the comments section, in case you have further questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top