Skip to main content
HomeTutorialsR Programming

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.
Updated Mar 2020  · 4 min read

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

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

Check out our Principal Component Analysis in R Tutorial.

Topics

R Courses

Course

Introduction to R

4 hr
2.7M
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
Related

Data Science in Finance: Unlocking New Potentials in Financial Markets

Discover the role of data science in finance, shaping tomorrow's financial strategies. Gain insights into advanced analytics and investment trends.

Shawn Plummer

9 min

5 Common Data Science Challenges and Effective Solutions

Emerging technologies are changing the data science world, bringing new data science challenges to businesses. Here are 5 data science challenges and solutions.
DataCamp Team's photo

DataCamp Team

8 min

Navigating R Certifications in 2024: A Comprehensive Guide

Explore DataCamp's R programming certifications with our guide. Learn about Data Scientist and Data Analyst paths, preparation tips, and career advancement.
Matt Crabtree's photo

Matt Crabtree

8 min

A Data Science Roadmap for 2024

Do you want to start or grow in the field of data science? This data science roadmap helps you understand and get started in the data science landscape.
Mark Graus's photo

Mark Graus

10 min

R Markdown Tutorial for Beginners

Learn what R Markdown is, what it's used for, how to install it, what capacities it provides for working with code, text, and plots, what syntax it uses, what output formats it supports, and how to render and publish R Markdown documents.
Elena Kosourova 's photo

Elena Kosourova

12 min

Introduction to DynamoDB: Mastering NoSQL Database with Node.js | A Beginner's Tutorial

Learn to master DynamoDB with Node.js in this beginner's guide. Explore table creation, CRUD operations, and scalability in AWS's NoSQL database.
Gary Alway's photo

Gary Alway

11 min

See MoreSee More