A Basic Introduction to
Language
08/26/2021 A Basic Introduction to R Language 1
VARIABLES
• Saved with .R extension
• A named storage that can be used by our program
• A Variable can be assigned a value in 2 ways –
var_name <- var_value
var_name = var_value
• To check the Data Type of Variable = class() function
• To see the list of variable = ls() function
• To delete a variable = rm() function
08/26/2021 A Basic Introduction to R Language 5
OPERATORS
• There are 2 types of Operators in R-
1) Unary Operators – Operate on one Operand/Variable
-4 , !variable
2) Binary Operators – Operate on two Operands/Variables
4+7, 4*2
• Further Classification –
1) Arithmetic Operators ( + , - , * , /)
2) Relational Operators (< , > , == , != , >= , <=)
3) Logical Operators ( ! , & , | , isTrue(variable) )
08/26/2021 A Basic Introduction to R Language 6
DECISION MAKING
• Decision making structures evaluates a condition and
depending on the result some other code is processed
• There are 3 Types of decision making statements in R
1) If Statements
2) If – Else
3) Switch Statements
08/26/2021 A Basic Introduction to R Language 7
DECISION MAKING
IF / IF- ELSE CONDITION
08/26/2021 A Basic Introduction to R Language 8
SWITCH CONDITION
switch(Expression/Condition,
case1, case2, case3....)
08/26/2021 A Basic Introduction to R Language 9
LOOPS
• The concept of executing a block of code Multiple times
• There are 3 Types of Loops in R
1) For Loop – executes statement multiple times, checks
condition at the end
2) Repeat Loop – repeats the code multiple times
3) While Loop – executes the code till a condition is
satisfied
08/26/2021 A Basic Introduction to R Language 10
LOOPS
FOR LOOP WHILE LOOP REPEAT LOOP
08/26/2021 A Basic Introduction to R Language 11
DATA STRUCTURES
VECTORS
LISTS
ARRAY
MATRIX
DATA FRAMES
08/26/2021 A Basic Introduction to R Language 12
VECTORS
A vector is simply a list of items that are of the same
type.
To combine the list of items to a vector, use
the c() function and separate the items by a comma.
# Vector of strings # Vector of numerical values
fruits<-c("banana", "apple", "orange") numbers <- c(1, 2, 3)
# Print fruits # Print numbers
fruits numbers
08/26/2021 A Basic Introduction to R Language 13
LISTS
A list in R can contain different data types inside it.
A list is a collection of data which is ordered and
changeable.
To create a list, use the list() function
# List of strings
thislist <- list("apple", "banana", "cherry")
# Print the list
thislist
08/26/2021 A Basic Introduction to R Language 14
MATRICES
• A matrix is a two dimensional data set with columns
and rows.
• A column is a vertical representation of data, while a
row is a horizontal representation of data.
• A matrix can be created with the matrix() function.
Specify the nrow and ncol parameters to get the
amount of rows and columns:
# Create a matrix
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
# Print the matrix
thismatrix
08/26/2021 A Basic Introduction to R Language 15
ARRAYS
Compared to matrices, arrays can have more than two
dimensions.
We can use the array() function to create an array, and
the dim parameter to specify the dimensions:
# An array with one dimension with values ranging from 1 to
24
thisarray <- c(1:24)
thisarray
# An array with more than one dimension
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray
How does dim=c(4,3,2) work?
The first and second number in the bracket specifies the amount of rows and
columns.
The last number in the bracket specifies how many dimensions we want.
08/26/2021 A Basic Introduction to R Language 16
DATA FRAMES
Data Frames are data displayed in a format as a table
Usually created by reading a dataset in excel
08/26/2021 A Basic Introduction to R Language 17