R is one of the most popular programming languages in data science and is widely used across various industries and academia. It’s open-source, easy to learn, and capable of handling complex data and statistical manipulations, making it the preferred computing environment for many data scientists today.
This cheat sheet will cover an overview of getting started with R.
2.1 Using Packages in R
R packages are collections of functions and tools developed by the R community. They increase the power of R by improving existing base R functionalities or adding new ones.
## Lets you install new packages (e.g., tidyverse package)#install.packages("tidyverse")##Lets you load and use packages (e.g., tidyverse package)library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.5.2 ✔ tibble 3.2.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.4
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
2.2 The Working Directory
The working directory is a file path that R will use as the starting point for relative file paths. That is, it’s the default location for importing and exporting files. An example of a working directory looks like “/file/path”
## Returns your current working directorygetwd()
[1] "/home/pawan/r-cheatsheets"
## Changes your current working directory to a desired file pathsetwd("~/")
2.3 Operators in R
2.3.1 Arithmetic Operators in R
Operator
Description
a + b
Sums two variables
a - b
Subtracts two variables
a * b
Multiply two variables
a / b
Divide two variables
a ^ b
Exponentiation of a variable
a %% b
The remainder of a variable
a %/% b
Integer division of variables
2.3.2 Relational Operators in R
Operator
Description
a == b
Tests for equality
a != b
Tests for inequality
a > b
Tests for greater than
a < b
Tests for smaller than
a >= b
Tests for greater or equal than
a <= b
Tests for smaller or equal than
2.3.3 Logical Operators in R
Operator
Description
!
Logical NOT
&
Element-wise Logical AND
&&
Logical AND
|
Element-wise Logical OR
||
Logical OR
2.3.4 Assignment Operators in R
Operator
Description
x <- 1, x = 1
Assigns a variable to x
2.3.5 Other Operators in R
Operator
Description
%in%
Identifies whether an element belongs to a vector
$
Allows you to access objects stored within an object
%>%
Part of magrittr package, it’s used to pass objects to functions
2.4 Getting Started with Vectors in R
Vectors are one-dimensional arrays that can hold numeric data, character data, or logical data. In other words, a vector is a simple tool to store data.
A vector is a contiguous collection of objects of the same type. Common types of vectors include logical, integer, double, and character.
2.4.1 Creating Vectors in R
A vector is a one-dimensional data set or a single-column data set, that doesn’t have a row
Here c called the command
Within the brackets, the things called elements, and in the starting of elements inside the square bracket [] is the position of the element or the indicate the row number
There are types of vector
Numeric vector - 1,2,3,4
Character vector - “A”, “a”, “b”, “ram”
Logical - True, False
Creates a vector using elements separated by commas
c(1,3,5)
[1] 1 3 5
Creates a vector of integers between two numbers
1:7
[1] 1 2 3 4 5 6 7
Creates a vector between two numbers, with a specified interval between each element
seq(2,8,by =2)
[1] 2 4 6 8
Creates a vector of given elements repeated a number of times
rep(2,8,times =4)
[1] 2 2 2 2 2 2 2 2
Creates a vector of given elements repeating each element a number of times