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.frame(studentID=1:5, year=c("1960","1980","1990","1998","2001"),score=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, times=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 |
which.max(vec) |
To find the position of the max value |
which.min(vec) |
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(matrix1,matrix2) |
To row bind matrix1 and matrix2 |
cbind(matrix1,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-dimensional array |
2D = array(1:20, dim = c(1,3)) |
To define a 2-dimensional array |
3D = array(1:20, dim = c(1,4,5)) |
To define a 3-dimensional array |
Hypothesis
Method |
Purpose |
aov() |
To find ANOVA or Analysis of Variance |
wilcox.test(data) |
To find the Wilcox test on the given data |
t.test(data) |
To find 1 sample t-test of the given data |
cor.test(data1,data2) |
To find the correlation test of the given data |
t.test(data1,data2) |
To find the 2 sample t-test of the given data |
chisq.test(data) |
To find the Chi-square test of the given data |
t.test(pre, post, paired=TRUE) |
To find the paired sample t-test of the given data |
shapiro.test(data) |
To find the Shapiro test of the given data |
Statistics and Descriptive Statistics
Method |
Purpose |
colSums(data[]) |
To find the column sum of a particular column of the given data |
rowSums(data[]) |
To find the row sum of a particular row of the given data |
summary(lm(y ~ x1 + x2 + x3, data=mydata)) |
To find the multiple regression of a given data |
cluster = kmeans(data) |
To find the kmeans cluster analysis of a given data |
summary(glm(y ~ x1 + x2 + x3, family="", data=mydata)) |
To find the classification of a given data |
colMeans(data[]) |
To find the column mean of a particular column of the given data |
rowMeans(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 coordinates of a given point |
ggplot(data = NULL, mapping = aes(), ...) |
To initialize a ggplot object of a given data |
geom_density() |
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=TRUE,...) |
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) |
Concatenate the vectors after converting to character |
tolower() |
Converts the given text to lower case characters |
toupper() |
Converts the given text to upper case characters |
toString(x) |
A helper function to produce a single character string |
substring(chr, n, n) |
To replace or retrieve the substring of a given string |
Probability
Method |
Purpose |
rexp(n) |
To find the Exponential distribution of n |
runif(n, min = 0, max = 1) |
To find the Uniform distribution of n |
rbinom(n, size, prob) |
To find the Binomial distribution of n |
rnorm(n, mean, sd) |
To find the Normal distribution of n |
rpois(n, size) |
To find the Poisson distribution of n |
Loops
Statement |
Purpose |
if(condition){ block of statements } else { block of statements } |
if-else statements format |
while(condition){ 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!