Select Page

Data types are used to define the size and type of the variable. In R, there is no need to declare a variable as some data types. The variables are assigned with R-objects and the R-object’s data type becomes the variable’s data type.

There are mainly 6 R-objects:

• Vectors
• Lists
• Arrays
• Matrices
• Data frames
• Factors

## Vectors

Vector is a sequence of data elements of the same type. It is a one-dimensional array. The simplest way to create a vector in R is to use the c() function.

Example:

Note:  You can use = or <- to assign the values to a variable.

Output:

When we execute the above script file, it will display the following output:

There are five classes of vectors:

• Numeric
• Integer
• Complex
• Logical
• Character
• Numeric Data Types

Decimal values are used for numeric data types. If you assign a numeric value for any variable x like given below, x will become a numeric type.

Example:

Output:

If we print the class name of x then:

Output will be:

Furthermore, even if we assign an integer value to a variable y, it is still being saved as a numeric value.

• Integer Data Types

To create an integer type of variable in R, you have to invoke the as.integer() function.

Example:

Output:

And if you check the class of y:

Output will be:

You can also append the L suffix to declare an integer.

Example:

Output:

• Complex data type

Complex value can be defined by the pure imaginary value ‘i’.

Example:

Output:

And if you check the class of k:

Output will be:

• Logical Data Types

Logical value is mostly created via comparison variables.

Example:

Output:

We can also check the class:

Output will be:

• Character Data Types

Character object is used to enter or represent string values in R.  To convert objects into character, we have to use the as.character() function within your code.

Example:

Output:

Output will be:

You can also assign some string values:

Example:

Output:

## Lists

List is a collection of R-objects which contains elements of different types like –strings, numbers, vectors and another list inside it.

Example:

Output:

## Matrices

Matrices are the R-objects, in which the two-dimensional rectangular data set are arranged. A matrix can be built using the matrix() function.

Syntax: matrix(data, nrow, ncol, byrow, dimensions)

Where, data: – it is the input vector which will be the data elements of the matrix

nrow: –  number of rows to be created

ncol:  number of columns to be created

byrow: – it is just a logical clue. If it is TRUE, then the input vector elements are arranged by row.

dimname: – it specifies the names assigned to the rows and columns.

Example 1:

Output:

Example 2:

Output:

## Arrays

Unlike matrices, arrays can be of any number of dimensions. It means they can store data in more than two dimensions. It also takes vectors as input. To create an array, it uses the dim parameter.

Example:

Let’s create an array with two elements with 3X3 matrices each.

Output:

## Factors

Factors are the R data objects. They are used to categorize the data and store it as levels. They can store both integers and strings. Factors are used in data analysis for statistical modeling. factor() function is used to create factors.

Example:

Output:

## Data Frames

A data frame is a table or a two-dimensional array like structure in which each column contains values of one variable (means the first column can be a character, the second column can be numeric and thirds column can be logical, etc.) and each row has one set of values from each column. To create a data frame, we have to use data.frame() function.

Output: