Data types in R With Examples

Before we delve deeper into R programming, it is important to understand the various data types used in R. R has the following basic data types:

  • Character
  • Numeric
  • Integer
  • Logical
  • Complex

Apart from these basic types, R also supports several data structures that are constructed from the basic types. These are:

  • Vectors
  • Matrices
  • Arrays
  • Dataframes
  • Lists
  • Factors

This tutorial will help you understand the basic data types. The working of each complex data structure will be covered in more elaborate tutorials.

Numeric Data Types in R

Since R is predominantly a language designed for statistical purpose, numeric and integer data types are widely used. We will begin with the most frequently used numeric data type. As the name indicates, numeric corresponds to any numerical value. These values can be floating-point, decimal or double.

Before we assign our first variable, please note that the assignment operator in R is <- . Less than symbol followed by a hyphen.

Begin by assigning random numeric values to variables a, b, and c. The typeof() function in R gives how the variable is stored internally, whereas the class() function tells us what data type class this variable belongs to. R also offers some checking functions starting with “is“. The function is.numeric() when called upon a variable returns a Boolean value of whether the object is of the numeric data type. There are respective is functions for other classes as well.

Run the following code in your script editor. Place the cursor on the first line and execute each line pressing Ctrl+ Enter. Also observe how these variables start to become a part of your environment window as you run each line.


Note how all the variables are stored as double in the internal storage, irrespective of being defined with floating-point or not, as shown by the typeof() function. The variable a with value 3 is stored as 3.0, a double value. The function call is.integer() returns a FALSE Boolean value because of this. The class(a) line gives our variable’s type numeric.

Data Types in R


Without any specification a numerical value is treated as a numeric data type by R compiler. Therefore, to define an integer variable, it is necessary to specify so. This is done by using the as.integer() function.

Soon after you run the first line of the code, you see the value assigned to a become 3L, as opposed to 3. The suffix L signifies a long integer of range -2*10ˆ9 to +2*10ˆ9. The variable a is both an integer and a numeric, since all integers are numerics, but not vice versa.



The character data type in R is meant to be used for both strings and single characters. A character can be simply specified by using a single or double quotes pair as below.


An integer or a numeric type can be converted into a character using as.character() function.



Logical is the data type used to store Boolean values TRUE and FALSE that arise as a result of logical operations. We have already encountered the TRUE and FALSE outputs above as we called the is.numeric() and is.integer() functions above. Let us verify this using the following statement.


Similarly, variables of logical type can be used to work with all logical operations.



The final basic data type we discuss here is the complex type. This is used to represent complex numbers in mathematics. Complex numbers are of the form a+bi where a and b are integers.


If we wish to calculate the square root of -1, which is mathematically known as i, the imaginary component of the imaginary number, We cannot calculate it by using normal sqrt (square root) function.


This is possible by defining -1 as a complex number first.


This ends our discussion of the basic data types in R.

By admin

Leave a Reply

%d bloggers like this: