Show Menu

Base R Cheat Sheet by

A cheat sheet about R programming language

Language itself

R is a free software enviro­nment for statis­tical computing and graphics.
It's a compiled language.
R is a dynami­cal­ly-­typ­ed,that means the type of a variable is determined at runtime, and that variables do not have to be explicitly declared with a specific type.
R is weakly­-typed, which means that it will automa­tically convert between types when necessary, without the need for explicit type casting.
R is a garbag­e-c­oll­ected language. This means the R runtime system automa­tically manages the memory used by the program.
R is both a functional an object oriented language but it doesn't support inheri­tance, polymo­rphism, and encaps­ula­tion.
In R, functions call by value. This means that when a function is called, the values of the arguments passed to the function are passed to the function's local scope, and any changes made to the arguments within the function do not affect the original values of the arguments outside the function.

Language enviro­nment

To code in R you can install the extension in your IDE like VSC or install its own IDE R Studio :
R for Windows
R for Linux
Then to run it you can run it with the IDE if you don't have one you can use R Studio server, which is a web-based version of R Studio that can be accessed through a web browser. The command for R Studio is
Rscript myscript.R
When installed R Studio it would have a minimal impact impact on the local machine but if you runs large R scripts or using R for data analysis and machine learning tasks, which require a lot of memory,it may have a bigger impact on the local machine. But in the end it does not modify something in your machine that would block you in it's use.


: Download and install a package from CRAN.
: Load the package into the file.
: Use a function from the package

Commonly used library
stringr for string manipu­lation
dplyr for data frame manipu­lation
ggplot2 for plotting graphs
lubridate for date

To go further

R is an integrated suite of software facilities for data manipu­lation, calcul­ation and graphical display. It includes:
-An effective data handling and storage facility,
-A suite of operators for calcul­ations on arrays, in particular matrices,
-A large, coherent, integrated collection of interm­ediate tools for data analysis,
-Graphical facilities for data analysis and display either on-screen or on hardcopy, and
-A well-d­eve­loped, simple and effective progra­mming language which includes condit­ionals, loops, user-d­efined recursive functions and input and output facili­ties.
The people who mainly use R are data scientist / engineer

Syntax and data structures

Variable Assignment
a <- 'bonjour' 
: string
a <- 6
: integer

For loop :
for (variable in sequence){ Do something }

While loop :
while (condi­tion){ Do something }

if (condi­tion){ Do something } else { Do something different }

a == b
: Are equal
a > b
: Greater than
a >= b
: Greater than or equal to
: Is missing
a != b
: Not equal
a < b
: Less than
a <= b
: Less than or equal to
: Is null

 functi­on_name <- function (var){ Do something return (new_v­ari­able) }

Maths Functions
: Natural log /
: Sum
: Expone­ntial /
: Mean
: Largest element /
: Median
: Smallest element /
: Percentage quantiles
round(x, n)
: Round to n decimal places
: Rank of elements
signif(x, n)
: Round to n signif­icant figures
: The variance /
cor(x, y)
: Correl­ation
: The standard deviation

l <- list(x = 1:5, y = c('a', 'b'))
: A list is collection of elements which can be of different types.
: second element of l
: new list with only the first element
: element named x
: new list with only element named y

paste(x, y, sep = ' ')
: Join multiple vectors together.
paste(x, collapse = ' ')
: Join elements of a vector together.
grep(p­attern, x)
: Find regular expression matches in x.
gsub(p­attern, replace, x)
: Replace matches in x with a string.
: Convert to uppercase.
: Convert to lowercase.
: Number of characters in a string.

m <- matrix(x, nrow = 3, ncol = 3)
: Create a matrix from x.
df[ , 2]
: Select a column .
df[2, ]
: Select a row.
df[2, 2]
: Select an element.

Data Frames
df <- data.f­rame(x = 1:3, y = c('a', 'b', 'c'))
: A special case of a list where all elements are the same length.
: See the full data frame.
:See the first 6 rows.
: Number of rows.
: Number of columns.
: Number of columns and rows.
As the same sub settings as matrixes and lists

: plot values of x in order
plot(x, y)
: plot values of x against y

Reading and writing data
df <- read.t­abl­e('­fil­e.txt')
df <- read.c­sv(­'fi­le.c­sv') 
/ Read a file text or a csv
write.t­ab­le(df, ‘file.t­xt’)
write.c­sv(df, ‘file.c­sv’)
/ Write a file text or a csv


No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Base JavaScript Cheat Sheet

          More Cheat Sheets by Todin

          Base JavaScript Cheat Sheet