In our previous tutorial (Syntax and Basics for R Programming) we had an insight into a simple hello world program that gives you a brief idea of how to use RStudio, basic syntax, and how comments are written in R. This tutorial takes you to one of the fundamental basics of R i.e. the variables and constant concept in R. If you have any prior knowledge to python, Go (Python, Go) programming languages it will be a great advantage, within a short time you will be masters in R. The others don’t get panic R is a simple language that anybody can learn like a toddler learning to walk. So let’s jump to our tutorial and walk together.
Variables are reserved memory locations to store data values or vectors or a list of values or a dataset or object in R. Like in all other programming languages, there should be some space inside memory locations that can hold the different values that are being used in R programming. These memory locations are represented by a name that is called later in the program for referring that address space and using the corresponding value contained by it. Whenever a variable is created in the R program corresponding memory location to store the value contained by that variable is reserved. In other words, a memory location is reserved while creating a variable.
Once a variable is created during the time of execution of the R program it identifies the datatype (type of data stored inside variable may be an integer (3), float (3.9), string (“R”)) of variable and allocates with a corresponding memory location.
Note : Just understand the concept of variables in R ignore the syntax of variables in the picture you are going to learn that in upcoming sections of this tutorial.
In the R programming language, three types of operators support in declaration and initialization of variables. The three operators are as follows
The syntax for assigning variables with values using the above operators.
//Assignment Operator (=)
<variable_name> = <value >
//Leftward Operator (<-)
<variable_name> <- <value>
//Rightward Operator (->)
<value> -> <variable_name>
site = "Welcome to Learn eTutorials" #using = operator
print(site)
language <- "R programming " # using <- operator
print(language)
"Variables in R" -> Tutorial #using -> operator
print(Tutorial)
Output:
[1] "Welcome to Learn eTutorials" [1] "R programming " [1] "Variables in R"
In the above program, a variable site
is created of string data type. The value “Welcome to Learn eTutorials” is assigned to site variable using “=
” equal to or assignment operator. Similarly, another variable language is created which holds the value "R programming " .In these lines of codes leftward operator (“ <-
”) assigns value to variable language. In the last lines of codes, you can see the program begins with initializing value "Variables in R" followed by a rightward operator “->
” which assigns value to the variable Tutorial.
Let us look at the snippet to see how these codes really works
Let us begin from the scratch with the classic hello world program. You are going to create a variable hello world of string datatype and print the resultant statement. In order to edit our code easily, we will begin by familiarizing it with RStudio together with variables.
Create a variable named VARIABLE that store a value “hello world!!!” of string data type .In R programming language to assign a value to a variable we use a different operators which we are going to discuss in this tutorial later. Let us see a simple example using “=” assignment operator that assigns value “hello world!!!” to VARIABLE and print () function displays output.
VARIABLE = "Hello world"
print(VARIABLE)
The following are the naming conventions followed in R programming language
Valid Convention | Invalid Conventions | Description |
---|---|---|
site_123, Site, site,SiteName,Sitename | Available nomenclature | |
Language_1 | _language, Language$1 | Beginning with underscore (_) and symbols other than dot, _ not allowed. |
Language1 | 1language | Beginning with digit is not allowed |
.site_123 | .123_site | Beginning with dot is allowed but dot followed by digits are not allowed. |
VARIABLE = "Hello world 1"
print(VARIABLE)
var1 = "Hello world 2 "
print(var1)
variable_1 = "Hello world 3"
print(variable_1)
VariableName = "Hello world 4"
print(VariableName)
varaible_name ="Hello world 5"
print(varaible_name)
_123variable = "Hello world 6"
print(_123variable )
Output:
[1] "Hello world 1"] "Hello world 2 " [1] "Hello world 3" [1] "Hello world 4" [1] "Hello world 5" > print(_123variable ) Error: unexpected input in "print(_" >
Look at the snippet to visualize the code for better understanding.
The scope is defined as a lifetime of a variable. A scope is an area under which a variable is alive or applicable. Consider there is a function called sample()
. Inside the function sample()
there is a variable called local_variable. The scope of the variable local_variable is within the function sample()
only. This means the lifetime of local_variable is within that function. Suppose any other function defined outside the sample()
function tries to access this variable declared local to sample()
function, it cannot access it. Because the variable local_variable is not visible outside the block. This is what is meant by the scope of a variable. Whereas a variable declared outside the sample() function known as global_varaiable is easily accessible within the function as well as outside the function.
Note: A scope is a region or segment where a variable is declared, initialized, and used. When this region or block terminates from its execution the variable gets destroyed automatically.
Variable scope types | Description |
---|---|
Global variables | Variables that are accessible and visible from any part of the program. The lifetime of a variable is throughout program execution. Variables can be declared inside or outside of a function. Variables can be modified from any region of code may be from inside or outside of a function. |
Local variables | Variables that are accessible only within a function or code of segments. The lifetime or scope of a variable is limited within that function. Variables are locally declared inside a function. Variables can be modified inside the function only. |
In the below-given program, a variable named global_variable is declared with an initialized value of 45. The program contains a function sample()
, inside the function is provided with the print statement for global_variable which displays the output as shown below. Later in the program outside the function sample()
, the global_variable value is updated with “hello”.
The example proves that the scope of the global variable is inside as well as outside of a function from where the globally declared variables are accessible and visible.
# R program to declare global variables
# global variable
global_variable = 45
# global variable accessed from a function sample()
sample = function(){
print(global_variable)
}
sample()
# modified global variable value
global_variable= "hello"
sample()
Output:
[1] 45 [1] "hello"
In the same program we discussed earlier here a local_variable is declared and assigned with the value “hello I am local variable" inside the function sample()
. The program executes and displays the value and later the value of local_variable is modified with "hello my value got modified" within the function sample()
itself.
Local variables are only locally accessible and can be modified only within a function where it is defined.
# R program to declare local variables
# local variable accessed within a function sample()
sample = function(){
local_variable <- "hello i am local variable"
print(local_variable)
# modified local variable value
local_variable <-"hello my value got modified"
print(local_variable)
}
Output:
[1] "hello i am local variable" [1] "hello my value got modified"
Like other programming languages, R programming also has a rich set of built-in functions also called methods. Methods are functions that are already defined to perform a specific task that the user requires. Everything in R is done through a method or function. For instance, print() is a function used to print the given text on the console. Similarly, R has hundreds and hundreds of methods to perform a designated task. Here in this section, we are focusing on some of the common methods associated with variables, their usage, and syntax. See below table :
Method | Description | Syntax |
---|---|---|
class() | Determines the data type of variables | class(variable_name) |
Is() | To check currently available variables | Is(variable_name) |
rm() | To remove/delete a variable | rm(variable_name) |
paste() | Concatenate elements | paste(varaible_1,varaible_2) |
In the R programming language data type of variables can be determined by using a built-in function supported by the R package or standard libraries. The built-in function or method that supports identifying data type of variable is class()
.
class(variable_name)
• Keyword class defines class() to identify data type of variable
site = "Welcome to Learn eTutorials" #using = operator
print(site)
language <- "R programming " # using <- operator
print(language)
"Variables in R" -> Tutorial #using -> operator
print(Tutorial)
cat("The class of variable site is ",class(site),"\n")
cat(class(language),"\n")
cat("The class of Tutorial is ", class(Tutorial) , "\n ")
Output:
[1] "Welcome to Learn eTutorials" [1] "R programming " [1] "Variables in R" The class of variable site is character character The class of Tutorial is character
In the R programming language, the available variables are determined using a built-in function called ls().
ls(variable_name)
• Keyword Is defines ls() function to check available variables in R program
"language" "site.1" "site.2" "Tutorial"
These are the available or alive variables displayed by the below program during the execution of the program.
site.1 = "Welcome to Learn eTutorials" #using = operator
print(site.1)
site.2 = "Learn eTutorials"
print(site.2)
language <- "R programming " # using <- operator
print(language)
"Variables in R" -> Tutorial #using -> operator
print(Tutorial)
print(ls()) #To find available variables
Output:
[1] "Welcome to Learn eTutorials" [1] "Learn eTutorials" [1] "R programming " [1] "Variables in R" [1] "language" "site.1" "site.2" "Tutorial"
In the R programming language, the unnecessary variables or variables of no functionality can be removed or deleted from a program by using a built-in function called rm()
.
rm(variable_name)
• Keyword rm defines rm() built in function that removes a variable.
These are the available or alive variables displayed by the below program during the execution of the program.
site.1 = "Welcome to Learn eTutorials" #using = operator
print(site.1)
site.2 = "Learn eTutorials"
print(site.2)
language <- "R programming " # using <- operator
print(language)
"Variables in R" -> Tutorial #using -> operator
print(Tutorial)
Output:
[1] "Welcome to Learn eTutorials" [1] "Learn eTutorials" [1] "R programming " [1] "Variables in R"
Let us add the below given built-in function rm()
to delete a variable, where the parentheses enclose the name of a variable to be removed from the program.
rm(site)
print(site)
When these lines of codes are added the variable named site
gets removed from the program. You can see the corresponding output below after the execution of the program.
[1] "Welcome to Learn eTutorials" [1] "Learn eTutorials" [1] "R programming " [1] "Variables in R" Warning message: In rm(site) : object 'site' not found Error in print(site) : object 'site' not found Execution halted
R programming language supports a built-in function known as a paste()
to concatenate one or more variables in a program. Two variables A & B are declared with values "Learn R in" & "Learn eTutorials" respectively. In this program, values are assigned to variables A & B using a leftward operator.
A <- "Learn R in"
B <- "Learn eTutorials"
paste(A, B)
When the program gets executed the built-in method paste() concatenate the two variables A & B to produce the output as shown below
[1] "Learn R in Learn eTutorials"
R supports single line assignment for multiple variables with same values. The three variables named first_variable , second_variable ,third_variable is assigned with same value using leftward operator with value "helloo it is easy right!!!!"
# "Multiple value in single line"
first_variable <- second_variable <- third_varaible <- "helloo it is easy right!!!!"
# Print values of variables
cat ("The first variable is ",first_variable,"\n")
cat ("The first variable is ",second_variable,"\n")
cat ("The first variable is ",third_varaible,"\n")
Output:
The first variable is helloo it is easy right!!!! The first variable is helloo it is easy right!!!! The first variable is helloo it is easy right!!!!
R supports two types of variables: