Skip to main content
HomeAbout RLearn R

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

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.

R Courses

Introduction to R

BeginnerSkill Level
4 hr
2.5M
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

Free Access Week | Aug 28 – Sept 3

Access DataCamp's entire platform for free, including all 440+ courses, for an entire week. No catch, no credit card required—just unlimited learning for anyone with internet access.

Will Rix

5 min

How to Choose The Right Data Science Bootcamp in 2023 (With Examples)

Learn everything about data science bootcamps, including a list of top programs to kickstart your career.
Abid Ali Awan's photo

Abid Ali Awan

10 min

DataCamp Portfolio Challenge: Win $500 Publishing Your Best Work

Win up to $500 by building a free data portfolio with DataCamp Portfolio.
DataCamp Team's photo

DataCamp Team

5 min

A Data Scientist’s Guide to Signal Processing

Uncover actionable insights hidden in complex signal data by filtering noise, choosing appropriate visualizations, finding patterns in the time- and frequency-domain, and more using signal processing.
Amberle McKee's photo

Amberle McKee

25 min

Chroma DB Tutorial: A Step-By-Step Guide

With Chroma DB, you can easily manage text documents, convert text to embeddings, and do similarity searches.
Abid Ali Awan's photo

Abid Ali Awan

10 min

Introduction to Non-Linear Model and Insights Using R

Uncover the intricacies of non-linear models in comparison to linear models. Learn about their applications, limitations, and how to fit them using real-world data sets.

Somil Asthana

17 min

See MoreSee More