R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Global vs. local assignment operators in r (‘<<-’ vs. ‘<-’).

Posted on September 11, 2022 by Trevor French in R bloggers | 0 Comments

Understanding the difference between local and global assignment operators in R can be tricky to get your head around. Here’s an example which should clear things up.

r global variable assignment

First, let’s create two variables named “global_var” and “local_var” and give them the values “global” and “local”, respectively. Notice we are using the standard assignment operator “<-” for both variables.

Next, let’s create a function to test out the global assignment operator (“<<-”). Inside this function, we will assign a new value to both of the variables we just created; however, we will use the “<-” operator for the local_var and the “<<-” operator for the global_var so that we can observe the difference in behavior.

This function performs how you would expect it to intuitively, right? The interesting part comes next when we print out the values of these variables again.

From this result, we can see the difference in behavior caused by the differing assignment operators. When using the “<-” operator inside the function, it’s scope is limited to just the function that it lives in. On the other hand, the “<<-” operator has the ability to edit the value of the variable outside of the function as well.

r global variable assignment

Global vs. local assignment operators in R (‘ was originally published in Trevor French on Medium, where people are continuing the conversation by highlighting and responding to this story.

Copyright © 2022 | MH Corporate basic by MH Themes

Never miss an update! Subscribe to R-bloggers to receive e-mails with the latest R posts. (You will not see this message again.)

Global vs. local assignment operators in R ( <<- vs. <- )

Understanding the difference between local and global assignment operators in R can be tricky to get your head around. Here’s an example which should clear things up.

r global variable assignment

First, let’s create two variables named “global_var” and “local_var” and give them the values “global” and “local”, respectively. Notice we are using the standard assignment operator <- for both variables.

Next, let’s create a function to test out the global assignment operator ( <<- ). Inside this function, we will assign a new value to both of the variables we just created; however, we will use the <- operator for the local_var and the <<- operator for the global_var so that we can observe the difference in behavior.

This function performs how you would expect it to intuitively, right? The interesting part comes next when we print out the values of these variables again.

From this result, we can see the difference in behavior caused by the differing assignment operators. When using the <- operator inside the function, it’s scope is limited to just the function that it lives in. On the other hand, the <<- operator has the ability to edit the value of the variable outside of the function as well.

IMAGES

  1. Global vs. local assignment operators in R

    r global variable assignment

  2. r-How the global assignment affect inside the function?(Especially

    r global variable assignment

  3. Intro to R Programming Variables, Variable Naming Rules & Assignment Operator

    r global variable assignment

  4. Alcance de la variable en R

    r global variable assignment

  5. How to set global variables in R Markdown?

    r global variable assignment

  6. Dynamic variable assignment in R

    r global variable assignment

VIDEO

  1. R Programming

  2. Introduction to R: Variables

  3. Variables in R Programming

  4. Declaring Variables in R

  5. Python 3 Programming Tutorial

  6. NI LabVIEW: Global variables