R – Variables

A variable is a memory allocated for 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.

Variables in R

R is a dynamically typed language, i.e. the 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.

Declaring and Initializing Variables
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.

Syntax:

#using equal to operator
variable_name = value



#using leftward operator
variable_name <- value

#using rightward operator
value -> variable_name

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate 
# Initialization of variables
  
# using equal to operator 
var1 = "hello"
print(var1)
  
# using leftward operator
var2 <- "hello"
print(var2)
  
# using rightward operator
"hello" -> var3
print(var3)

chevron_right


Output:

[1] "hello"
[1] "hello"
[1] "hello"

Nomenclature of Variables
The following rules need to be kept in mind while naming a variable:

  • A valid variable name consist of a combination of alphabets, numbers, dot(.) and underscore(_) characters. Example: var.1_ is valid
  • Apart from dot and underscore operator no other special character is allowed. Example: var$1 or var#1 both are invalid
  • Variables can start with alphabets or dot character. Example: .var or var is valid
  • Variable should not start with numbers or underscore. Example: 2var or _var is invalid.
  • If a variable starts with dot the next thing after dot cannot be a number. Example: .3var is invalid

Important Methods for Variables
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:

  • class() function
    This builtin 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    var1 = "hello"
    print(class(var1))

    chevron_right

    
    

    Output:

    [1] "character"
    
  • ls() function
    This builtin 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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # using equal to operator 
    var1 = "hello"
      
    # using leftward operator
    var2 <- "hello"
      
    # using rightward operator
    "hello" -> var3
      
    print(ls())

    chevron_right

    
    

    Output:

    [1] "var1" "var2" "var3"
    
  • rm() function
    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # using equal to operator 
    var1 = "hello"
      
    # using leftward operator
    var2 <- "hello"
      
    # using rightward operator
    "hello" -> var3
      
    # Removing variable
    rm(var3)
    print(var3)

    chevron_right

    
    

    Output:

    Error in print(var3) : object 'var3' not found
    Execution halted 
    

Scope of Variables

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: 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.
  • Local Variables: 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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.