• Articles
  • Tutorials
  • Interview Questions
  • Webinars

Basics of Data Structures with R Cheat Sheet

R Data Structures User Handbook

Data Structures are used to organize and store the data on the computer. R programming is a language that supports particular types of Data Structures. Most of the industries use Data Structures and write them in particular programming languages such as R.

Data Structures in R cheat sheet will help you with the basic concepts and the commands one must know to get started with it. It is helpful for beginners as well as experienced people as it provides a quick overview of the important concepts required.

You can also download the printable PDF of this Data Structures in R cheat sheet

Data structure in R Cheat Sheet

Data Structure: It is a way of organizing data that contains the items stored and their relationship with each other
R Programming: It is a programming language that is mainly used by Data Scientists, it is preferred by people who are good at Statistics and mathematics. In this language functions and codes are stored in a package inside the library

Types of R objects:

  • Vector: The basic data structure in R is Vector, it comes in two parts
    • Atomic vector and
    • List
    • A basic way of using vectors is by the c () function. E.g.: C (1,2,3)
  • Matrix: A matrix is a collection of numbers arranged into an affixed number of rows and columns. By using a matrix function we can reproduce a memory representation of the matrix in R
  • Array: In R it is called a multi-dimensional data structure. Here, the data is stored in the form of matrices. Array in R is the data object which can store data in more than two dimensions
  • List: These are the objects which contain elements of different types like strings, numbers, vectors, and other lists inside it. It can be created using the list () function
  • Data Frames: It refers to the tabular form of data, representing the cases (rows), each of which consists of the number of observations or measurements (columns). It is used for storing data tables, it is a list of vectors of equal length

Data tables:

It extends and enhances the functionality of Data Frames
Data tables

Types of Data Structures in R

Syntax for the use of R data structures:

Vector:

  • To create a vector:
v1 < - c (1,2,3)
  • Get length
length(v1)
  • Check if all or any is true
all(v1); any(v1)
  • Integer indexing
v1[1:3]; v1[c (1<6)]
  • Boolean indexing
v1[is.na(v1)] < - 0
  • Naming:
c(first = ‘a’, ..) or names(v1) < -c(‘first’, ..)

List:

  • To create the list:
list1 < - list (first = ‘a’, …)
  • Create empty list
vector (mode = ‘list’ , Length = 3)
  • Get element:
list1[[1]] or list1[[‘First’]]
  • Append using numeric index
list1 [[6]] < - 2

Data frame:

  • To create data frame:
df1 < - data.frame (col1=v1, col2=v2, v3)
  • Dimension:
nrow(df1); ncol(df1); dim(df1)
  • Get/set column names
rownames(df1)
rownames(df1) < - c(…)
  • Preview
head(df1,  n=10) ; tail(…)
  • Get data types:
class(df1) # is data.frame
  • Index by columns
df1[‘col1’] or df1[1]
df1[ c(‘col1’, ‘col3’)] or df1[ c(1,3)]
  • Index by rows or columns
df1[ c(1,3), 2:3]
# returns data from rows 1,3 and columns 2,3
  • To create data table from data.frame:
data.table(df1)
  • Index by columns:
dt1[, ‘col1’ , with= FALSE] or dt1[, list (col1)]
  • Show info for each data.table in memory:
tables()
  • Show keys in data.table:
key(dt1)
  • Create index for col1 and reorder data according to col1:
setkey(dt1,col1)
  • Use key to select data:
dt1[c(‘col1value1’, ‘col1value2’,]
  • Multiple key select:
dt1[J(‘1’, c(‘2’, ‘3’)), ]
  • Aggregation:
dt1[, list(col1=mean(col1)), by = col2 ]
dt1[, list(col1=mean(col1), col2sum= sum(col2)), by = list(col3, col4) ]

Matrix:

  • To create Matrix:
matrix1 < - matrix(1:10, nrow = 5 )
# fills rows 1 to 5, column 1 with 1:5, and column 2 with 6:10
  • Matrix multiplication:
matrix1 %*% t (matrix2)

# where t() is transpose

Download a Printable PDF of this Cheat Sheet

With this, we come to an end of Data Structures in R Cheatsheet. To get in-depth knowledge, check out our data Manipulation with R programming, Data visualization in R tutorial, advanced analytics topics like regressions, and data mining using RStudio. You will work on real-life projects and assignments to master data analytics.

About the Author

Principal Data Scientist

Meet Akash, a Principal Data Scientist with expertise in advanced analytics, machine learning, and AI-driven solutions. With a master’s degree from IIT Kanpur, Aakash combines technical knowledge with industry insights to deliver impactful, scalable models for complex business challenges.