What are the Variables in R
Variables are the storage units for data that can be changed during the program's execution. They are symbolic representations of the data formats. In R, variables are only made when assigned values. There is no 'only declaration' of variables in R.
Example:-
name <- "India"
print(name)
abc = c("Coding", "Ninjas")
print(abc)
def <- c(0,1,2)
print(def)
g=c("This", "is", "Coding", "Ninjas")
j=c(8,9)
e=list(g,j)
print(e)
Output

In the above code, variables' abc,' ‘g,' 'c,' and 'e' are initialized using the '=' or equal operator. While variables 'def' and 'name' are initialized using the '<-' or leftward operator.
Note: The leftward operator(<-) can be used anywhere in the program, whereas the equal operator(=) is only used at the top level(like in the complete expression typed in the command prompt).
Here, some values are assigned using the 'c()' function. The 'c()' function combines elements into a vector in the R programming language.
Naming Conventions of Variables in R
In the R Programming Language, there are specific rules for naming variables. They are as follows.
-
The names of the variables can include alphabets and numbers as well as underscore(_) and period(.).
-
The name of a variable cannot start with an underscore or a number.
-
If a variable’s name starts with a period(.), it cannot be immediately followed by a number.
-
The names of the variables are case-sensitive.
-
Reserved words like TRUE, if, etc., cannot be used to name a variable.
Following are some examples of valid variable names.
aa24. = "You"
Bus_stand <- "Delhi"
.my2 ="g"
Following are some examples of invalid variable names.
24aa = "You"
.2 ="l"
_j="o"
TRUE = "p"
Scope of Variables in R
The scope of a variable is the block area in the code where it is accessible. There are two types of variable scopes: local variables and global variables.
Local Variables
These are the variables that are accessible(or those which exist) only in a particular block of code(like in a specific function). Outside the particular function, these variables are not accessible and are released.
Global Variables
These variables are accessible throughout the whole program. These variables are accessible from any part of the program.
Now, let us study these two in detail.
Local Variables
A local variable is a variable that is defined inside a function or a block of code where it can be accessed. It cannot be accessed outside of the function in which it is initialized.
Let us understand this by the following program in R.
myfunc <- function(){
myvar = "Hello Coders!"
print(myvar)
}
myfunc()
Output
[1] "Hello Coders!"
Here, we initialized a variable named 'myvar' inside a function named 'myfunc'. When we call that function, the local variable gets printed because it is accessed inside.
Now, let us see one more example as follows.
myfunc <- function(){
myvar = "Hello Coders!"
}
print(myvar)
Output
Error in eval(ei, envir) : object 'myvar' not found
When we run the code mentioned above, an error occurs. It is because 'myvar' was initialized inside 'myfunc'. Thus, it is accessible inside the function only.
Let us see one more example.
myfunc <- function(){
myvar = "Hello Coders!"
onemorefunction = function(){
print(myvar)
}
onemorefunction()
}
myfunc()
Output
[1] "Hello Coders!"
Here, 'onemorefunction' is nested inside the function 'myfunc'. Thus, a local variable initialized inside a function is accessible inside all the nested functions inside that function.
Global Variables
A global variable is a variable that is accessible throughout the whole program. Let us see its functioning with some examples.
Let us see the following example.
myvar = "Hello Coders!"
myfunc <- function(){
print(myvar)
}
myfunc()
Output
[1] "Hello Coders!"
Hare, ‘myvar’ is initialized globally, making it accessible throughout the program.
Let us see one more example where we can see the scope of local and global variables in one program.
myvar = "Hello Coders!"
myfunc <- function(){
myvar <- "Hello there!"
print(myvar)
}
print(myvar)
myfunc()
Output
[1] "Hello Coders!"
[1] "Hello there!"
Here, 'myvar' is a global variable that gets printed as it is when called outside the function. But inside the function, another 'myvar' is initialized as a local variable and gets printed.
Now, let us see how to access a global variable inside a function to change it with the following program.
myvar = "Hello Coders!"
myfunc <- function(){
myvar <<- "Hello there!"
print(myvar)
}
myfunc()
print(myvar)
Output
[1] "Hello there!"
[1] "Hello there!"
Here, we use the '<< -' or the super assignment operator to access and update the value of a global variable inside a function. Hence, in the above program, when we use this operator, it searches for the variable locally, and when it is not found locally, it searches in the global environment(R_GlobalEnv). Finally, when the variable is still not found, it creates a global variable, and we can change the value of 'myvar' globally.
Frequently Asked Questions
What is R programming language?
The R programming language allows you to analyze data, machine learning, data science models, etc. It helps you create and modify packages, functions, etc. It is an open-source programming language. It is in high demand nowadays.
What is a variable in programming?
A variable is a value that, depending on different conditions or information, can change. They represent specific types of data formats symbolically.
What do you mean by the scope of a variable?
The scope of a variable means the area in the program where that particular variable can be accessed. Like, whether it is accessible throughout the program or only in a defined function.
What is the crucial difference between static and dynamic scoping?
In static scoping, the program first searches for the variable in the current function, then in the field of global variables, and then in smaller scopes. In dynamic scoping, the program refers to the variable in the most recent function, then in the calling functions.
What do you mean by a local variable?
A local variable is a kind of variable that is accessible only in some specific area of the code. It is accessible in the block of code where it is declared.
Conclusion
In this article, we studied variables in R. We briefly introduced them and their naming conventions. Finally, we learned about the scope of variables in R.
If you want to enhance your knowledge about this topic, read the following articles:-
To learn more about DSA, competitive coding, and many more knowledgeable topics, please look into the guided paths on Coding Ninjas Studio. Also, you can enroll in our courses and check out the mock test and problems available. Please check out our interview experiences and interview bundle for placement preparations.
Happy Coding!