Skip to main content

Definitive Guide: Variables in R Tutorial

In this tutorial, you'll learn all about R variables including how to define variables, remove variables, and much more.
Mar 2020  · 4 min read


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('.').


      • Underscore('') at the beginning of the variable name are not allowed.


        '_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.


      '.myvar' is a valid variable name. However, '.1myvar' is not a valid variable name because the period followed by a number is not valid.
  1. Reserved words or keywords are not allowed to be defined as a variable name.
  2. 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

        my.var2 = 8.5

    • 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)
  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:

    Returns: complex

    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:


      Returns 'symbol' whereas


      Returns 'name'.

    • type "language" in typeof() is returned as "(" or "call" in mode()
    • For Example:


      Returns 'language' whereas


      Returns 'call'.

    • type "integer" and "double" in typeof() is returned as "numeric" in mode()
    • For Example:


      Returns 'integer' whereas


      Returns 'numeric'.

    • type "special" and "builtin" in typeof() is returned as "function" in mode()
    • For Example:


      Returns 'special' whereas


      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

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


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


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


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.

Check out our Principal Component Analysis in R Tutorial.

R Courses

Introduction to R

4 hr
Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.
See DetailsRight Arrow
Start Course
See MoreRight Arrow

Inside Our Favorite DataFramed Episodes of 2022

An inside look at our favorite episodes of the DataFramed podcast of 2022

Adel Nehme

2 min

[Infographic] Data Science Project Checklist

Use this checklist when planning your next data science project.
Adel Nehme's photo

Adel Nehme

Introduction to Probability Rules Cheat Sheet

Learn the basics of probability with our Introduction to Probability Rules Cheat Sheet. Quickly reference key concepts and formulas for finding probability, conditional probability, and more.
Richie Cotton's photo

Richie Cotton

1 min

Data Governance Fundamentals Cheat Sheet

Master the fundamentals of data governance with our Data Governance Fundamentals Cheat Sheet. Quickly reference key concepts, best practices, and key components of a data governance program.
Richie Cotton's photo

Richie Cotton

1 min

ChatGPT Cheat Sheet for Data Science

In this cheat sheet, gain access to 60+ ChatGPT prompts for data science tasks.
Travis Tang's photo

Travis Tang

10 min

T-tests in R Tutorial: Learn How to Conduct T-Tests

Determine if there is a significant difference between the means of the two groups using t.test() in R.
Abid Ali Awan's photo

Abid Ali Awan

10 min

See MoreSee More