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.
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.
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.
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
VIDEO