Home » R Programming » Data Structures in R » Data Structures in R

# Data Structures in R

In this tutorial, you will learn about what are different data structures in R? To understand the R language, you will need to understand the basic data types and data structures and how to use them.

## Data Structures in R

A data structure is a way of organizing the data in such a way that we can use it more efficiently in R programming language.

In R, there are five basic data structures.

• Vector
• matrix
• array
• data frame
• list

Basically these data structures are classified into two types, namely, atomic (homogeneous) and generic (heterogeneous).

In atomic data structure all the stored objects are of the same type, whereas in generic data structure all the stored objects may be mixture of different types.

Dimension Atomic Generic
1 Vector List
2 Matrix data frame
n Array

Each object in R has a class or data type. Class determines how functions treat them. There are five basic classes of an objects.

• Integer
• Numeric (Real numbers)
• Logical (True/False)
• Character
• Complex

data types in R.

### Vectors in R

Vectors is a basic data structure in R. Vector is an atomic (homogeneous) data structure, where all the stored objects are of same type. Vector can be defined as a set of constants or scalars of same type arranged in a one-dimensional array.

### Matrix in R

A matrix is a two-dimensional data structure in R. It is a set of elements appearing in rows and columns. Matrix in R is atomic (homogeneous). That is all the elements of the matrix must be of same mode (logical, numeric, complex or character).

### Arrays in R

Arrays in R is an atomic (homogeneous) data structure. Matrix is two dimensional object. When there are more than two dimensions, we use array to store such a data. Thus arrays are similar to matrices, but have more than two dimensions.

### Data Frames in R

In R language, a data frame is a primary data structure for handling tabular data sets like a spreadsheet. Data frames is an atomic data structure in R. Data frames are like matrices except that the columns are allowed to be of different types, i.e., data frames stores heterogeneous data types whereas matrix stores homogeneous data types.

### Lists in R

Lists are the more general class of data storage. Lists are generic vectors where each element can be any type of object; e.g., a vector (of any mode), a matrix, a data frame or a function. Because of this flexibility, lists are the basis for most complex objects in R.