Tutorials
must read
r programming

Definitive Guide: Variables in R

In this tutorial, you'll learn all about R variables including how to define variables, remove variables, and much more.

Variables

Variables are the identifier or the named space in the memory, which are stored and can be referenced and manipulated later in the program. R is a Dynamically Typed and Interpreted language where Type Checking of the variable and other objects is done at the run time. It also means that the Interpreter of R doesn't force the programmer to explicitly declare the 'data-type' of the variable before its usage.

Rule Variable in R

  1. The variable name must start with letter and can contain number,letter,underscore('') and period('.').
  2. Example: variableName1,new.variable,
    • Underscore('') at the beginning of the variable name are not allowed.
    • Example: '_my_var' is not a valid variable name.
    • Period('.') at the beginning of the variable name are allowed but should not be followed by a number. It is preferable in R to use '.' which helps to separate the different words for the identifier.
    • Example: '.myvar' is a valid variable name. However, '.1myvar' is not a valid variable name because the period followed by a number is not valid.
  3. Reserved words or keywords are not allowed to be defined as a variable name.
  4. Special characters such as '#', '&', etc., along with White space (tabs, space) are not allowed in a variable name.

Variable Assignment

Variables in R can be assigned in one of three ways.

  1. Assignment Operator: "=" used to assign the value.The following example contains 20 as value which is stored in the variable 'first.variable' Example: first.variable = 20

  2. '<-' Operator: The following example contains the New Program as the character which gets assigned to 'second.variable'.
    Example: second.variable <- "New Program"

  3. '->' Operator: The following example contains 565 as the integer which gets assigned to 'third.variable'.
    Example: 565 -> third.variable

Variable Type

The type of variable in R can be determined by class(),typeof() and mode()

  1. 'class()'- will give the high-level type of an object, which is to say from the perspective of Object-Oriented Programming in R.Depending upon whether it is a vector or any other data structure, the return type will be different.

    • If the type of object is the vector, then one of the following data types is chosen.
      • Integer
      • Numeric
      • Character
      • Logical
      For example, the integer type and numeric type will be returned below.

      my.var1 = 8L
      Returns:'integer'

      my.var2 = 8.5
      Returns:'numeric'


    • However, with the objects like matrix(),data.frame(),list() and array() the respective type is returned. For example, the list type will be returned as "list" below.

      my.var3 = list(6,9,5)
      Returns:'list'
  1. typeof()- The return values are low-level, which matches the internal type of object in R.It is mostly used by R programmer to determine the type of an object.
    For Example, some of the internal types of objects in R are shown below:

    typeof(8+6i)
    Returns: complex

    typeof(factor(c(4,5,6)))
    Returns: integer

You can explore the Object internal type with typeof() to find the different internal type of an object.

3.mode()- It also returns the values which are the same and closely related to 'typeof().' However, there are some differences between 'typeof()' and mode() on the following types.

  • type "symbol" in typeof() is "name" in mode()
  • For Example: typeof(as.name('foo'))Returns 'symbol' whereas mode(as.name('foo')) Returns 'name'.

  • type "language" in typeof() is returned as "(" or "call" in mode()
  • For Example: typeof(y~x)Returns 'language' whereas mode(45L) Returns 'call'.

  • type "integer" and "double" in typeof() is returned as "numeric" in mode()
  • For Example: typeof(45L)Returns 'integer' whereas mode(45L) Returns 'numeric'.

  • type "special" and "builtin" in typeof() is returned as "function" in mode()
  • For Example: typeof(return)Returns 'special' whereas mode(45L) Returns 'function'.

Removing Variable

The variable in R can easily be removed or deleted by using 'rm()'.
For example, the output of the code below is 8.

my.var = 8
cat(my.var)

The variable is removed by using 'rm(my.var)'.

rm(my.var)

You can check whether the variable is removed or not through the help of 'cat(my.var)'

cat(my.var)

The above code will result in the following error:
Error in cat(my.var): object 'my.var' not found

Congratulations

Congratulations, you have made it to the end of this tutorial!

In this tutorial, you have covered R's variables, rules for defining variables, different types of assignment of the variable along with different kinds of checking the variable types, and removal of the variable.

If you would like to learn more about R, take DataCamp's Introduction to R course.