R is one of the most frequently used programming languages in the Machine Learning and Data Science fields and is used extensively in both academia and a variety of industries. Many data scientists now use R as their preferred computing environment since it is simple to learn, open-sourced, and capable of handling statistical computations and complex data.
Excited to learn R? In this article, we will be exploring an overview of R using an easy-to-understand cheat sheet. Use it as a convenient, high-level guide to starting with R immediately.
R Cheat Sheet
Here, we will explore the various shortcuts and symbols related to R.
Basic Syntax
Operator |
Purpose |
<- or = |
Assignment |
# |
Comment |
/ |
Division |
<<- |
Global Assignment |
%% |
Remainder |
* |
Scalar Multiplication |
%/% |
Integer Division |
%*% |
Matrix Multiplication |
v[1] |
First vector element |
Accessing Help
Function Name |
Purpose |
help.start() |
opens help |
class(df_name) |
Returns class of the given object |
?tidyverse |
Shows tidyverse package documentation |
str(df_name) |
Returns information and structure of the given object |
?function_name |
Shows in-built functions’ documentation |
??”some_input” |
Shows a given input’s documentation |
Dataframe
Method/Definition |
Description |
summary(df_name) |
Returns the statistics of data in a descriptive format |
view(df_name) |
Opens the editor |
df_name = data.fÂramÂe(sÂtudentÂID=Â1:5Â, year=cÂ("1960",Â"Â1980","1990Â"Â,"1998",Â"Â2001"),ÂscoÂre=Âc(6,1,Â3,2,2)) |
Dataframe definition |
Utility
Method |
Purpose |
order(index)Â |
To find the index to sort a vector |
apply(data, axis, function_name) |
To apply data to function in the particular axis |
data = read.csv(file.choose()) |
To read data from the file explorer |
dim() |
To find dimensions of matrix/dataframe/vector |
lapply(data, function_name) |
To apply the data to the function |
getwd() |
Gets the working directory |
length() |
To find the vector length |
install.packages(“package_name”) |
Installs the required R package |
names() |
Returns the column names |
setwd(“C:/file/path”) |
To set the current working directory |
rapply(data, function_name, how) |
Depending on the value of how, the data is applied to the function |
sort() |
To sort a vector |
rm(variable_name) |
To remove the variable |
detach(“package name”) |
Detaches the given package |
ls() |
To list all the variables |
library(“package name”) |
Makes contents of the given package ready to use |
Vector
Method |
Purpose |
range(vec) |
To find the range of a vector |
num = c(3,7,Â2,1Â,8,5) |
Defining a numeric vector |
rep(1:Â8, tÂimes=2) |
Replicates the elements of the vector by the given number of times |
sd(vec) |
To find the standard deviation of a vector |
chr = c("rte",Â"Âqhz Â") |
Defining a character vector |
var(vec) |
To find the variance of a vector |
log = c(FALSEÂ, ÂFALSE, TRUE) |
Logical vector |
whiÂch.mÂaxÂ(vec) |
To find the position of the max value |
which.mÂinÂ(veÂc) |
To find the position of the min value |
mean(vec) |
To find the mean of the values of a vector |
Matrix and Arrays
Method |
Purpose |
rbind(ÂmatÂrix1,matrix2) |
To row bind matrix1 and matrix2 |
cbind(ÂmatrixÂ1,matrix2) |
To column bind matrix1 and matrix2 |
mat = matrixÂ(1:15, nrow=3, ncol=5) |
To define a matrix |
1D = array(Â1:14) |
To define a 1-dimensÂional array |
2D = array(Â1:20, dim = c(1,3)) |
To define a 2-dimensÂional array |
3D = array(Â1:20, dim = c(1,4,5)) |
To define a 3-dimensÂional array |
Hypothesis
Method |
Purpose |
aov() |
To find ANOVA or Analysis of Variance |
wilcox.teÂst(Âdata) |
To find the Wilcox test on the given data |
t.testÂ(data) |
To find 1 sample t-test of the given data |
cor.teÂst(ÂdatÂa1,Âdata2) |
To find the correlÂation test of the given data |
t.testÂ(daÂta1Â,data2) |
To find the 2 sample t-test of the given data |
chisq.tÂesÂt(data) |
To find the Chi-square test of the given data |
t.testÂ(prÂe, pÂostÂ, paÂireÂd=TRUE) |
To find the paired sample t-test of the given data |
shapirÂo.tÂestÂ(data) |
To find the Shapiro test of the given data |
Statistics and Descriptive Statistics
Method |
Purpose |
colSumÂs(dÂata[]) |
To find the column sum of a particular column of the given data |
rowSumÂs(dÂata[]) |
To find the row sum of a particular row of the given data |
summarÂy(lm(y ~ x1 + x2 + x3, data=mÂydata)) |
To find the multiple regression of a given data |
cluster = kmeansÂ(data) |
To find the kmeans cluster analysis of a given data |
summarÂy(glm(y ~ x1 + x2 + x3, familyÂ="", data=mÂydata)) |
To find the classiÂficÂation of a given data |
colMeaÂns(Âdata[]) |
To find the column mean of a particular column of the given data |
rowMeaÂns(Âdata[]) |
To find the row mean of a particular row of the given data |
Visualization
Method |
Purpose |
geom_hist() |
To find the histogram of a given data |
coord_Âflip() |
To flip the x and y coordiÂnates of a given point |
ggplotÂ(data = NULL, mapping = aes(), ...) |
To initiaÂlize a ggplot object of a given data |
geom_dÂensity() |
To produce a density plot of a given data |
facet_Âgrid() |
To lay out panels in a grid of a given data |
geom_point() |
To produce scatter plots of a given data |
qplot(Âdata, line=TÂRUEÂ,...) |
To produce the quantile plot of a given data |
geom_bar() |
To produce a bar graph of a given data |
Strings
Method |
Purpose |
paste (…, sep = " ", collapse = NULL) |
ConcatÂenate the vectors after converting to character |
toÂlower() |
Converts the given text to Âlower case characters |
toupper() |
Converts the given text to Âupper case characters |
toStriÂng(x) |
A helper function to produce a single character string |
substrÂingÂ(chÂr, n, n) |
To replace or retrieve the substring of a given string |
Probability
Method |
Purpose |
rexp(n) |
To find the ExponeÂntial distriÂbution of n |
runif(n, min = 0, max = 1) |
To find the Uniform distriÂbution of n |
rbinom(n, size, prob) |
To find the Binomial distriÂbution of n |
rnorm(Ân, mÂean, sd) |
To find the Normal distriÂbution of n |
rpois(Ân, size) |
To find the Poisson distriÂbution of n |
Loops
Statement |
Purpose |
if(condiÂtion){ block of statements } else { block of statements } |
if-else statements format |
while(condiÂtion){ block of statements } |
while loop format |
for(variable in the sequence){ block of statements } |
for loop format |
Learn data exploration, data visualization, predictive analysis, R packages, data structures in R with the Data Science with R Certification. Check out the course now!
Want to learn more?
In this article, we covered all the basics of R with all the required shortcuts. To learn more about R and become a machine learning expert, check out Simplilearn’s Data Science with R Certification Course!