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

tutorial

Sorting Data in R

How to sort a data frame in R.
DataCamp Team's photo

DataCamp Team

2 min

tutorial

Merging Data in R

Merging data is a common task in data analysis, especially when working with large datasets. The merge function in R is a powerful tool that allows you to combine two or more datasets based on shared variables.
DataCamp Team's photo

DataCamp Team

4 min

tutorial

Scatterplot in R

Learn how to create a scatterplot in R. The basic function is plot(x, y), where x and y are numeric vectors denoting the (x,y) points to plot.
DataCamp Team's photo

DataCamp Team

tutorial

Operators in R

Learn how to use arithmetic and logical operators in R. These binary operators work on vectors, matrices, and scalars.
DataCamp Team's photo

DataCamp Team

4 min

tutorial

How to Transpose a Matrix in R: A Quick Tutorial

Learn three methods to transpose a matrix in R in this quick tutorial
Adel Nehme's photo

Adel Nehme

tutorial

Snscrape Tutorial: How to Scrape Social Media with Python

This snscrape tutorial equips you to install, use, and troubleshoot snscrape. You'll learn to scrape Tweets, Facebook posts, Instagram hashtags, or Subreddits.
Amberle McKee's photo

Amberle McKee

8 min

See MoreSee More