A data structure is a particular way of organizing data in a computer so that it can be used effectively. The idea is to reduce the space and time complexities of different tasks.
The two most important data structures in R are Matrix and Dataframe, they look the same but different in nature.
Matrix in R –
It’s a homogeneous collection of data sets which is arranged in a two dimensional rectangular organisation. It’s a m*n array with similar data type. It is created using a vector input. It has a fixed number of rows and columns. You can perform many arithmetic operations on R matrix like – addition, subtraction, multiplication, and divisions.
Example:
# Matrix of two rows # and three columns # boolean value by row is true. # passed as 4th parameter A = matrix (c(11, 22, 33, 44, 55, 66), nrow = 2, ncol = 3, byrow = 1) # Printing Matrix print(A) |
Output:
[, 1] [, 2] [, 3]
[1, ] 11 22 33
[2, ] 44 55 66
Application & Usage
- It has great use in Economics for calculating some data like GDP(Gross Domestic Production) or PI(Price per capita income).
- It is also helpful in the study of electrical and electronics circuits.
- Matrix are used in the study of surveys, i.e. Plotting graphs etc.
- Helpful in probablity and statistics.
DataFrames in R –
It is used for storing data tables. It can contain multiple data types in multiple columns called fields. It is a list of vector of equal length. It is a generalized form of a matrix. It is like a table in excel sheets. It has column and row names. The name of rows are unique with no empty columns. The data stored must be numeric, character or factor type. DataFrames are heterogeneous.
Example:
# creating company data frame comp.data <- data.frame( # company ids # data members comp_id = c (1:3), # company names comp_name = c("Geeks", "For", "Geeks"), growth = c(16000, 14000, 12000), # company start dates # data members comp_start_date = as.Date(c("02/05/10", "04/04/10", "05/03/10")) ) print(comp.data) |
Output :

Application & Usage
- Data frames can do lot of works like fit statistics formulas.
- Processing data(Not possible with Matrix, First converting to Data Frame is mandatory).
- Transpose is possible, i.e. changing rows to columns and vice versa which is useful in Data Science.
Matrix v/s Data Frames in R
| Matrix | Dataframe |
|---|---|
| Collection of data sets arranged in a two dimensional rectangular organisation. | Stores data tables that contains multiple data types in multiple column called fields. |
| It’s m*n array with similar data type. | It is a list of vector of equal length. It is a generalized form of matrix. |
| It has fixed number of rows and columns. | It has variable number of rows and columns. |
| The data stored in columns can be only of same data type. | The data stored must be numeric, character or factor type. |
| Matrix is homogeneous. | DataFrames is heterogeneous. |
Recommended Posts:
- How to randomly select rows from Pandas DataFrame
- Python | Change column names and row indexes in Pandas DataFrame
- Create pandas dataframe from lists using zip
- Different ways to create Pandas Dataframe
- Python | Create a Pandas Dataframe from a dict of equal length lists
- How to select multiple columns in a pandas dataframe
- Reset Index in Pandas Dataframe
- Get unique values from a column in Pandas DataFrame
- How to drop one or multiple columns in Pandas Dataframe
- Apply function to every row in a Pandas DataFrame
- How to rename columns in Pandas DataFrame
- Adding new column to existing DataFrame in Pandas
- Create pandas dataframe from lists using dictionary
- Saving a Pandas Dataframe as a CSV
- Difference of two columns in Pandas dataframe
- Split a text column into two columns in Pandas DataFrame
- Change Data Type for one or more columns in Pandas Dataframe
- Getting frequency counts of a columns in Pandas DataFrame
- Creating a dataframe using Excel files
- Different ways to iterate over rows in Pandas Dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

