R – Variables

23 May, 2022
A variable is a memory allocated for the storage of specific data and the name associated with the variable is used to work around this reserved block. The name given to a variable is known as its variable name. Usually a single variable stores only the data belonging to a certain data type. The name is so given to them because when the program executes there is subject to change hence it varies from time to time.
R Programming Language is a dynamically typed language, i.e. the R Language Variables  are not declared with a data type rather they take the data type of the R-object assigned to them. This feature is also shown in languages like Python and PHP.
R supports three ways of variable assignment:
Using equal operator- data is copied from right to left.
Using leftward operator- data is copied from right to left.
Using rightward operator- data is copied from left to right.
#using equal to operator 
variable_name = value
#using leftward operator 
variable_name <- value
#using rightward operator 
value -> variable_name
R
# R program to illustrate# Initialization of variables # using equal to operatorvar1 = "hello"print(var1) # using leftward operatorvar2 < - "hello"print(var2) # using rightward operator"hello" -> var3print(var3)
Output: 
[1] "hello"
[1] "hello"
[1] "hello"
The following rules need to be kept in mind while naming a variable: 
A valid variable name consists of a combination of alphabets, numbers, dot(.), and underscore(_) characters. Example: var.1_ is valid
Apart from the dot and underscore operators, no other special character is allowed. Example: var$1 or var#1 both are invalid
Variables can start with alphabets or dot characters. Example: .var or var is valid
The variable should not start with numbers or underscore. Example: 2var or _var is invalid.
If a variable starts with a dot the next thing after the dot cannot be a number. Example: .3var is invalid
R provides some useful methods to perform operations on variables. These methods are used to determine the data type of the variable, finding a variable, deleting a variable, etc. Following are some of the methods used to work on variables:
This built-in function is used to determine the data type of the variable provided to it. The variable to be checked is passed to this as an argument and it prints the data type in return.
Syntax:
class(variable) 
Example: 
R
var1 = "hello"print(class(var1))
Output: 
[1] "character"
This built-in function is used to know all the present variables in the workspace. This is generally helpful when dealing with a large number of variables at once and helps prevents overwriting any of them.
Syntax: 
 ls() 
Example: 
R
# using equal to operatorvar1 = "hello" # using leftward operatorvar2 < - "hello" # using rightward operator"hello" -> var3 print(ls())
Output: 
[1] "var1" "var2" "var3"
This is again a built-in function used to delete an unwanted variable within your workspace. This helps clear the memory space allocated to certain variables that are not in use thereby creating more space for others. The name of the variable to be deleted is passed as an argument to it.
Syntax: 
 rm(variable) 
Example: 
R
# using equal to operatorvar1 = "hello" # using leftward operatorvar2 < - "hello" # using rightward operator"hello" -> var3 # Removing variablerm(var3)print(var3)
Output:
Error in print(var3) : object 'var3' not found
Execution halted 
The location where we can find a variable and also access it if required is called the scope of a variable. There are mainly two types of variable scopes:
Global variables are those variables that exist throughout the execution of a program. It can be changed and accessed from any part of the program.
As the name suggests, Global Variables can be accessed from any part of the program.
They are available throughout the lifetime of a program.
They are declared anywhere in the program outside all of the functions or blocks.
Declaring global variables: Global variables are usually declared outside of all of the functions and blocks. They can be accessed from any portion of the program.
R
# R program to illustrate# usage of global variables # global variableglobal = 5 # global variable accessed from# within a functiondisplay = function(){print(global)}display() # changing value of global variableglobal = 10display()
Output:
[1] 5
[1] 10
In the above code, the variable ‘global’ is declared at the top of the program outside all of the functions so it is a global variable and can be accessed or updated from anywhere in the program.
Local variables are those variables that exist only within a certain part of a program like a function and are released when the function call ends. Local variables do not exist outside the block in which they are declared, i.e. they can not be accessed or used outside that block.
Declaring local variables: 
Local variables are declared inside a block.
R
# R program to illustrate# usage of local variables func = function(){# this variable is local to the# function func() and cannot be# accessed outside this functionage = 18   print(age)} cat("Age is:\n")func()
Output:
Age is:
[1] 18
kumar_satyam
sweetyty
Picked
R-Variables
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.