0% found this document useful (0 votes)
12 views16 pages

Module 3 - R Programming

This document outlines the objectives and exercises for Module 3 of BIA 5303 Big Data 2, focusing on R programming. Key topics include handling NA values, input/output functions, conditional statements, and loop implementations in R. Exercises are provided to practice these concepts using the airquality data frame.

Uploaded by

iremmidillic
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Module 3 - R Programming

This document outlines the objectives and exercises for Module 3 of BIA 5303 Big Data 2, focusing on R programming. Key topics include handling NA values, input/output functions, conditional statements, and loop implementations in R. Exercises are provided to practice these concepts using the airquality data frame.

Uploaded by

iremmidillic
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BIA 5303 Big Data 2

Module 3

R Programming Reference:
Tilman M. Davis, The Book of R,
Objectives no starch press, 2016

✓ Handling NA values in data sets.


✓ Use input and formatted output in R scripts.
✓ Apply conditions in R using if statements.
✓ Use switch function to implement multi-path selection in a compact way.
Lecturer
✓ Implement loops in R using for, while, and repeat statements. Anas Kuzechie
NA
✓ In statistical analyses, data sets often contain missing values.
✓ Identifying and handling missing values is important so that we can still use the
rest of the data.
✓ R provides a standard special term to represent missing values, NA, which reads as
Not Available.

✓ We can identify NA elements using the function [Link](). This is useful for
removing or replacing NA values.
✓ We can also use which() function to extract the indexes of NA values.
✓ Function [Link]() will take a variable and delete all NAs from it.
Exercise
✓ Consider the Ozone variable of data frame airquality. The variable contains
37 NAs out of 153 values.

✓ Determine the NAs in Ozone.

✓ Determine the index position of the NAs.


Exercise
Logical error! NA is returned if
✓ Find the average ozone: variable contains NAs.

Logical operator NOT.

Exclude NAs from computation.

✓ Create a vector that contains all the values of variable Ozone for the month of
May. Exclude all NAs:

5 NA values.
Input & Output
✓ readlin() function enables the script to read from the console.

✓ cat() function (concatenate and print) enables the script to output to the console.
format() function enables the
output value to be formatted.

✓ Example

casting string to integer

✓ We could also use the print() function and the


paste() function to display text and values.
if Function
✓ Syntax:

✓ Example R script that checks if a person's age is greater or equal to 18.

if-else Statement
✓ Syntax:

Note the
location of else
if-else if-else Statement
✓ Syntax:

✓ Example
ifelse Function
✓ ifelse() is a conditional function in R that allows us to perform element-wise
conditional operations on vectors or data frames.
✓ Syntax: ifelse(test_expression, x, y)

✓ Examples

✓ Exercise With reference to airquality data frame and Wind variable for the month
of May, generate a vector having element values "WINDY" if the wind speed is
greater than 15 mph. Otherwise, set "Not windy" value.
Exercise
switch Function
✓ The switch() function tests an expression against elements of a list. If the value
evaluated from the expression matches an item from the list, the corresponding
value is returned.

✓ Syntax: switch(expression, list)

✓ Examples
for Loop
✓ Syntax:

✓ Example

✓ Nested loops:
Exercise

✓ Using a for loop, Write an R script to calculate the average wind speed in May for
data frame airquality.

✓ Repeat the above for variable Ozone.


Exercise
✓ Write a script to create the matrix
shown in the figure.

✓ Next, using a nested for loop, the script


computes and displays the average grade
of each student (see output).
while Loop
✓ Syntax:

✓ Examples
break and next Statements
✓ break statement forces an exit from the loop.

✓ next statement forces code to jump to loop head to continue with next iteration.
repeat Statement
✓ Syntax:

You might also like