Professional Documents
Culture Documents
Bdo Co1 Session 4
Bdo Co1 Session 4
Session - 4
AIM OF THE SESSION
INSTRUCTIONAL OBJECTIVES
LEARNING OUTCOMES
Objects of R
Every programming language has its own data types to store values or any
information so that the user can assign these data types to the variables and
data types.
These data types can be character, integer, float, long, etc. Based on the data
than data types in R programming. The following are list of objects used in R
1. Vectors
2. List
3. Matrices
4. Factors
5. Data Frames
SESSION DESCRIPTION
VECTOR
Atomic vectors can store homogeneous data types such as character, doubles,
print(z) -------- 5
print(class(z)) -------- numeric
SESSION DESCRIPTION
List
List
Lists are designed to store heterogeneous data, meaning that each element
within a list can be of a different data type, and they can be of varying
lengths. This flexibility makes lists particularly useful for organizing and
Example
Output
my_list <- list(
name = "John Doe", $name
age = 30, [1] "John Doe"
city = "New York", $age
hobbies = c("Reading", "Hiking", "Cooking"), [1] 30
scores = c(95, 89, 78, 92) $city
) [1] "New York" $
print(my_list) Hobbies
[1] "Reading" "Hiking" "Cooking“
$scores
[1] 95 89 78 92
SESSION DESCRIPTION
Matrices
A matrix is a two-dimensional data structure that stores elements of the same data
type in rows and columns. Matrices are commonly used for various mathematical
Creation of Matrix in R
A matrix in R using the matrix() function. It takes several arguments, such as data,
Example
my_matrix <- matrix(1:9, nrow = 3, ncol = 3)
print(my_matrix)
Output
SESSION DESCRIPTION
Factors
Categorical data consists of distinct categories or levels, and factors are used
to store and manipulate this type of data. Factors are particularly useful for
variables appropriately.
SESSION DESCRIPTION
Creating Factors:
You can create a factor using the factor() function. This function takes a
# Create a factor
Levels:
A factor consists of a set of levels, which are the distinct categories or values
in the categorical variable. You can access the levels of a factor using the
levels() function.
factor_levels
Data Frames:
table.
Data frames are a common way to organize and work with structured data,
You can create a data frame using the data.frame() function. This function
allows you to combine vectors of different types into a data frame, with each
str(df)
SESSION DESCRIPTION
Data Manipulation:
• subset(): Filter rows based on conditions and select specific columns.
• merge() and join() (from dplyr): Perform data joins between data frames.
• stack() and unstack(): Reshape data from wide to long and vice versa.
String Manipulation:
Data Sorting:
Data Reshaping:
• **gather() and spread() (from tidyr): Reshape data from wide to long and vice
versa.
SESSION DESCRIPTION
Data Sampling:
• sample_n() and sample_frac() (from dplyr): Randomly sample rows from a data
frame.
Statistical Analysis:
Function Application:
Data I/O:
• read.csv(), read.table(): Read data from CSV files or tab-delimited text files.
Data Visualization:
• plot(), hist(), barplot(), etc.: Create basic plots.
• ggplot2 package: Create complex and customized plots.
• lattice package: Create conditioned plots.
• heatmap(), boxplot(), qqnorm(), etc.: Generate specialized plots.
• plotly and shiny for interactive plots.
SESSION DESCRIPTION
Control Structures
Control structures in R are fundamental programming constructs that
allow you to control the flow of your code and make decisions based
on conditions.
Allows you to test multiple conditions and execute different code blocks
based on which condition is true
Syntax:
if (condition1) { # Code to execute if condition1 is true}
else if (condition2) { # Code to execute if condition2 is true}
else { # Code to execute if no conditions are true}
Functions in R
A function is a block of code which only runs when it is called. You can pass
result.
In R, you define functions using the function() keyword and implement them
Call Function
function_name(arg,agr):
SESSION DESCRIPTION
return(result)
function_name(arg1,agr2)
SESSION DESCRIPTION
Example
square <- function(x) {
result <- x^2
return(result)
}
square(5)
Output
25
SELF-ASSESSMENT QUESTIONS
(a) Blocks are evaluated until a new line is entered after the closing brace
(b) Break will execute a loop while a condition is true
Text Books:
Reference Books:
Team – BDO