R代写-S263F
时间:2021-09-29

STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 1 Tutorial 1: Introduction to Big Data Learning Outcome:  Understanding the role of R in Big Data Analytics  Overview of R Studio  Creating a R Program  Data Manipulation in R What is R and its role in Big Data Analytics R is an open source software package to perform statistical analysis of data and glean key insights from data using mechanisms, such as regression, clustering, classification, and text analysis and is widely used by data scientist statisticians and others who need to make statistical analysis. R is registered under GNU (General Public License). It was developed by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, which is currently handled by the R Development Core Team. It can be considered as a different implementation of S, developed by Johan Chambers at Bell Labs. There are some important differences, but a lot of the code written in S can be unaltered using the R interpreter engine [1]. R provides a wide variety of statistical, machine learning (linear and nonlinear modeling, classic statistical tests, time-series analysis, classification, clustering) and graphical techniques, and is highly extensible. R has various built-in as well as extended functions for statistical, machine learning, and visualization tasks such as:  Data extraction,  Data cleaning  Data transformation  Statistical Analysis  Predictive modelling  Data visualization The strengths of R lie in its ability to analyse data using a rich library of packages. R can now connect with other data stores, such as MySQL, SQLite, MongoDB, Spark for Big Data Analysis. Overview of R Studio Desktop RStudio is an Integrated Development Environment (IDE) for R, a programming language for statistical computing and graphics. 1.1 Installation of R studio desktop at home R studio has been installed in our PC laboratories for lecture and tutorial use. However, you are strongly recommended to install R studio on your own PC or laptop for self-learning purpose. Follow the following videos to install R and Rstudio on Microsoft Windows/ MacOS, respectively. STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 2 Figure 1 How to download R and install Rstudio on Windows 10 2021 https://www.youtube.com/watch?v=NZxSA80lF1I Figure 2 How to install R & RStudio on Mac in 2021 - step-by-step walkthrough https://www.youtube.com/watch?v=LanBozXJjOk Features of R Programming Language Support GPU and Distributed Computing Many statistical functions and libraries STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 3 Graphics and Data visualization Databases Overview of Rstudio 1. Console The console window (in RStudio, the bottom left panel) is the place where you can type command, and it will show the results of a command. You can type commands directly into the console, but they will be forgotten when you close the session. 2. Script Editor Alternatively, you can enter the commands in the script editor, and save the script. This way, you have a complete record of what you did, you can also save and run the script. You can copy-paste into the R console. 3. Environment The Environment tab in the top right window lists the variables and functions present in the current R session. It does not include the function/data in loaded packages however (unless you select a package from the drop down menu that says “Global Environment”). When you ask “what have I created so far”, the answer is in the environment tab. STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 4 4. File Browser The default tab in the lower right window is a basic file browser. You can open, delete, and rename files there. It is not as well-developed as your operating system’s file browser and is mostly there so you don’t have to switch applications to manage files. 5. Plots Shows the plot generated from your Rscripts or R commands typed in the console after a plot routine is executed. 6. Packages Lists all the packages you have installed. Click the “install” button for installing new packages. Click “update” for updating packages Installing and Importing R packages First, you have to identify the name of the packages to be installed. Let’s consider gplots as an example. Part I – Getting the Package onto your Computer 1. Type “install.packages(“gplots”)” and then press the Enter/Return key. STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 5 2. If you have already loaded a package from a server in the R session, then R will automatically install the package. If not, R will automatically prompt you to choose a mirror. Again, choose one close to unless you want to watch a loading bar slowly inch its way to fulfillment. Part II – Loading the Package into R 1. Type “library(ggplot2)” and then press the Enter/Return key. Working directories There are different ways to find/change the working directory, i) Using the graphical user interface and ii) Typing R commands. STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 6 Part I. Using the graphical user interface 1. Go the file browser 2. Create folder: Creates a new folder for your R codes. 3. Delete file: Tick the box next to a file (e.g. “hello. R”) and click “delete” to delete the file. 4. Rename file: Tick the box next to a file (e.g. “hello. R”) and click “rename” to rename the file. 5. Path to displayed directory: Under the new folder button, there is a bar showing the current directory. 6. Change directory: Click the button “…” on the right hand side of the path to displayed directory and a window will show up for you to choose a directory. Part II. Using R commands The following table lists some of the useful R commands for working directories STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 7 Command Action getwd() Find the current working directory (where inputs are found and outputs are sent). setwd(‘your directory path’) Change the current working directory. Getting started with R console 1. To begin, click the console tab in the bottom left hand corner of R studio. 2. We shall begin with a simple variable assignment to get familiarized with the R environment. At the console prompt, enter the following to assign the string ‘apple’ to the variable ‘a’ a <- ‘apple’ 3. Then, enter the following to show the value inside the variable ‘a’ a Command Action ls() List all variables in the environment. rm(x) Remove x from the environment. rm(list = ls()) Remove all variables from the environment. # Single line comments Note: R commands are case-sensitive. Type Ctrl+L to clear the screen of console. STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 8 Getting started with Script editor 1. To begin, click the + button to create a new Rscript. A dropdown will appear and click R Script. 2. We shall repeat what we have tried in the previous console example in the Script Editor. Click the white empty space next to Line 1 and a cursor will appear. Enter following to assign the string ‘apple’ to the variable ‘a’ a <- ‘apple’ 3. Then, enter the following to show the value inside the variable ‘a’ a 4. Click the save button to save the Rscript. A file browser will appear. Save the file to the current working directory in *.R format. Then click the run button. STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 9 5. In the environment tab (right hand side of Rstudio), the variable ‘a’ and its value “apple” should appear. R markdown R Markdown is a file format for making dynamic documents with R. An R Markdown document is written in markdown (an easy-to-write plain text format) and contains chunks of embedded R code. Fig. 1.1 shows a R markdown file generated from Ex 1.1. Fig. 1.1 R Markdown file of Example 1.1. STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 10 1. To begin, go to File -> New File -> R Markdown 2. You can choose between exporting to html, pdf or MS word file. Type the title and author of your R markdown file, choose html and then click OK 3. Delete summary(cars) and plot(pressure) and replace with your own R codes. In R markdown, a way to display R codes is to display in chunks that lies within ```{r } ````. Options can be added too. For example, echo=False will prevent source code from being displayed. STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 11 4. Delete all the descriptions and modify all necessary places 5. Click the Knit button to export the R markdown to a html file. 6. The output html will look like this STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 12 Vectors Operations In R, vectors do not come with column/row attribute. When it is multiplied with a matrix using the command %*%, R will interpret the vector in whichever way makes the matrix product conformable. 1. Creating Vectors Command Action Output c(2,4,6) Join elements into a vector 2 4 6 2:6 An integer sequence 2 3 4 5 6 seq(2,3, by=0.5) A complex sequence 2.0 2.5 3.0 rep(1:2, times=3) Repeat a vector 1 2 1 2 1 2 rep(1:2, each=3) Repeat elements of a vector 1 1 1 2 2 2 2. Vector functions Command Action sort(x) Return x sorted in ascending order sort(x, index.return =TRUE) Return x sorted together with the index as $ix sort(x, decreasing =TRUE) Sort x in descending order table(x) See counts of values rev(x) Return x reversed unique(x) See unique values length(x) Return dimension of vector Random number generators rnorm(4) Generate a vector of dimension 4 with random numbers generated from normal distribution with mean 0 and variance 1 rpois(10,1) Generate a vector of dimension 10 with random numbers generated from a poison distribution with mean count of 1 sample(1:100,3,replace=TRUE) Generate a vector of dimension 3 with random numbers generated from uniform distribution with replacement 3. Selecting Vector Elements Command Action By Position STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 13 x[4] The fourth element x[-4] All but the fourth x[2:4] Elements two to four x[-(2:4)] All elements except two to four x[c(1,5)] Elements one to five. By Value x[x==10] Elements which are equal to 10 x[x<0] All elements less than zero x[x %in% c(1,2,5)] Elements in the set 1,2,5. Named Vectors x[‘apple’] Element with name ‘apple’ IMPORTANT !! Before you begin each chapter, always start a new project. 1. Go to File -> New Project 2. Choose New Directory STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 14 3. Choose New Project 4. After typing your directory name and choosing the subdirectory, click Create Project STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data Most materials are from reference books. For use in lectures only. Not for duplication Page | 15 Example 1.1 Vectors In the following example, we shall practise vector operations in R. Take a look at the following program with the line numbers at the left as follows: 1. # Example 1.1 2. x<-rnorm(10) 3. y2<-x[5] 4. y3<-x[-8] 5. y4<-x[2:6] 6. y5<-x[-(1:3)] 7. y6<-x[c(1,3,5)] 8. y7<-x[x<0] Each line represents a whole R statement. The first line # Example 1.1 is called a comment, which is used to document a program and to make the program readable and understandable. Comments are not interpreted in R and do not cause the computer to run. Currently, R only support single- line comment. At line 2, rnorm(10) generates a 10 dimension vector containing random numbers drawn from normal distribution with zero mean and variance 1. x<-rnorm(10) assigns the vector to a new variable called x. Lines 3-8 concerns with data manipulation within the vector x. At line 3, y2<-x[5] extracts the 5th element from vector x and stores them to a new variable called y2. At line 4, y3<-x[-8] extracts all elements from vector x except the 8th element and stores them to y3. y4<-x[2:6] in line 5 extracts the 2nd to 6th elements and store them to y4. y5<-x[-(1:3)] in line 6 collects all elements from vector x except 1st to 3rd elements and store them to y5. At line 7, y6<-x[c(1,3,5)] collects the 1st, the 3rd and the 5th elements and store them to y6. Finally, y7<-x[x<0] extracts elements in vector x that are smaller than zero and store them to y7. Tutorial Assignment 1.1 Vectors 1. Create a random vector of integers uniformly distributed from 1 to 20 and store it as vector x. 2. Extract the 3rd element of the vector x and store it as y2. 3. Extract all elements of x excluding the 5th element and store them as vector y3. 4. Extract the 6th – 10th elements for x and store them as vector y4 5. Extract all elements of x excluding the 6th – 10th elements and store them as vector y5. 6. Extract the 2nd, 5th and 11th element from x and store them as vector y6. 7. Extract elements of x which are larger than 11 and store them as vector y7. 8. Save the file as























































































































































































































































































































































































































-Ex_11.R. For example, if your ID is 11223344
and your name is Chan Tai Man, then save the file as 11223344-ChanTaiMan-Ex_1_1.R
STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data

Most materials are from reference books. For use in lectures only. Not for duplication Page | 16
Matrices and Data Frames

Matrices and Data Frames are widely used in R. Here is a list of commands.

4. Matrices
Command Action
A[2, ]

Select a row
A[ ,1 ]

Select a column
A[ 2,3 ]

Select an element
A[ ,-4 ] All but except the 4th column
cbind(df,y)
Add new column y to df

rbind(df,y)
Add new row y to df
Reshaping matrix
matrix(c,nrow=2) Reshape a vector c into 2 rows
E.g. Generating 8 random numbers with rnorm and
reshape into a 2x4 matrix.
matrix(rnorm(8),nrow=2)
matrix(c,ncol=2) Reshape a vector c into 2 columns
E.g. Generating 8 random numbers with rnorm and
reshape into a 4x2 matrix.
matrix(rnorm(8),ncol=2)
Naming the columns and rows
rownames(A)<-c(“Hi”,”Low”) Rename the two rows of A as “Hi” and “Low”
colnames(A)<-c(“A”,”B”,”C”,”D”) Rename the four columns of A as “A”, “B”, “C”, “D”

STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data

Most materials are from reference books. For use in lectures only. Not for duplication Page | 17
5. Data Frames

Command Action Output
df<-data.frame(x=1:3,
y=c(‘a’,’b’’c’))
Creates a dataframe with
columns x and y and store to df

df[ , 2] Extracts the 2nd column from df

df[2, ] Extracts the 2nd row from df

df[2, 2 ] Extracts an element from df

df$x Extract the column with the
name x from df

data.matrix(df) Extract matrix from dataframe
nrow(df) Number of rows
ncol(df) Number of columns
dim(df) Number of rows and columns

Example 1.2 Matrices and Data Frames

Take a look at the following program with the line numbers at the left as follows.

1. # Example 1.2
2. A<- matrix(rnorm(8),nrow=2)
3. B2<- A[,4]
4. B3<- A[,-5]
5. B5<- A[,6:9]
6. b<- rnorm(4)
7. d<- rnorm(2)
8. X<- rbind(A,b)
9. Y<- cbind(A,d)
10. rownames(A)<-c(“Hi”,”Low”)
STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data

Most materials are from reference books. For use in lectures only. Not for duplication Page | 18
First, we generate a 8 dimensional random vector drawn from normal distribution with zero mean
and variance 1 with rnorm(8) and reshape it to a 2x4 matrix with the matrix( ,nrow=2) command
and store it to A in line 1.

At line 2, B2<- A[,4] generates a 10 dimension vector containing random numbers drawn from
normal distribution with zero mean and variance 1. x<-rnorm(10) assigns the vector to a new
variable called x.

Lines 3-8 concerns with data manipulation within the vector x. At line 3, y2<-x[5] extracts the 5th
element from vector x and stores them to a new variable called y2. At line 4, y3<-x[-8] extracts all
elements from vector x except the 8th element and stores them to y3. y4<-x[2:6] in line 5 extracts
the 2nd to 6th elements and store them to y4. y5<-x[-(1:3)] in line 6 collects all elements from vector
x except 1st to 3rd elements and store them to y5. At line 7, y6<-x[c(1,3,5)] collects the 1st, the 3rd
and the 5th elements and store them to y6. Finally, y7<-x[x<0] extracts elements in vector x that
are smaller than zero and store them to y7.

Tutorial Assignment 1.2 Matrices and Data Frames

1. Create a random matrix of 3x6 drawn from normal distribution with mean 0 and variance 1 and
store as A.
2. Extract the 3rd row of the matrix A and store it as B2.
3. Extract all elements of A excluding the 5th column and store them as vector B3.
4. Extract the 2nd and 4th row from A and store them as vector B5.
5. Create a 6 dimensional vector b with sequence of integers from 1 to 6.
6. Create a 3 dimensional vector d with sequence of integers from 1 to 3.
7. Combine matrix A and vector b and store as matrix B6.
8. Combine matrix A and vector d and store as matrix B7.
9. Name the three rows of A as “Hi”,”Mid”,”Low”, respectively.
10. Save the file as -Ex_11.R. For example, if your ID is 11223344
and your name is Chan Tai Man, then save the file as 11223344-ChanTaiMan-Ex_1_2.R
Common arithmetic operations

Here is a list of common list of commands for Common arithmetic operations :

Command Action
Scalar operation
a+b Addition
a-b Subtraction
a*b Multiplication
STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data

Most materials are from reference books. For use in lectures only. Not for duplication Page | 19
a/b Division ( a divided by b)
a %% b Remainder from division of a with b
a %/% b Quotient from division of a with b
a ^b a to the power b
Matrix Algebra
A*B Elementwise multiplication
t(x) Transpose
A %*% B Matrix Multiplication
solve(A,b) Solves for x in A*x=b
solve(A) Calculates inverse of A
diag(x) If x is vector, it will create a diagonal matrix with elements of x in
the principal diagonal
diag(A) If A is matrix, it will return a vector containing all the elements in
the principal diagonal
rowMeans(A) Returns vector of row means.
rowSums(A) Returns vector of row sums.
colMeans(A) Returns vector of column means.
colSums(A) Returns vector of column sums.

Example 1.3 Common arithmetic operations

Take a look at the following program with the line numbers at the left as follows.

1. # Example 1.3
2. A<- matrix(rnorm(16),ncol=4)
3. B<- matrix(rnorm(8),nrow=4)
4. C1<- A %*%B
5. b<- rnorm(4)
6. C2<- solve(A,b)
7. C3<-diag(b)
8. C4<-diag(A)
9. C5<-rowMeans(B)
10. C6<-colMeans(B)

First, we generate a 16 dimensional random vector drawn from normal distribution with zero mean
and variance 1 with rnorm(16) and reshape it to a 4x4 matrix with the matrix( ,ncol=4) command,
which enforces the matrix to have 4 columns and store it to A in line 2. Similarly, we create another
matrix B<- matrix(rnorm(8),nrow=4) in line 3.
STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data

Most materials are from reference books. For use in lectures only. Not for duplication Page | 20
At line 3, C1<- A %*%B calculates the matrix multiplication A*B and stores them to C1. At line 5,
b<- rnorm(4) creates a 4 dimensional vector with elements drawn from normal distribution. C2<-
solve(A,b) in line 6 solve a system of linear equations denoted b A*x=b and save the answer of x
to C2. C3<-diag(b) in line 7 converts b into a diagonal matrix. C4<-diag(A) in line 8 extracts the
diagonal elements of A and store them as a vector in C4. C5<-rowMeans(B) take average over
each row of B and store as C5. C6<-colMeans(B) take average over each column of B and store
as C6.

Tutorial Assignment 1.3 Common arithmetic operations

1. Create a random matrix of 13x6 drawn from uniform distribution from 1 to 20 and store as A.
2. Create a random matrix of 6x2 drawn from uniform distribution from 1 to 20 and store as B.
3. Calculate A*B and store it as C1.
4. Create a 13 dimensional vector with random numbers drawn from normal distribution with zero
mean and variance 1 and store it as b.
5. Solve the systems of linear equation A*x=b and store the answer of x into C2.
6. Calculate the column mean of A.
7. Save the file as -Ex_11.R. For example, if your ID is 11223344
and your name is Chan Tai Man, then save the file as 11223344-ChanTaiMan-Ex_1_3.R
Basic Input and Output

Here are some common commands for basic input and output.

1. Reading and Writing from ‘txt’ and ‘csv’ files

Command Action
Import Data
df<-read.table(‘file.txt’) Reads a delimited txt file and writes to dataframe df
df<-read.csv(‘file.csv’)
Reads a comma separated value file and writes to
dataframe df
load(‘file.Rdata’) Reads from an R data file
Export Data
write.table(df, ‘file.txt’) Writes the dataframe df to a delimited txt file.
write.csv(df, ‘file.csv’)
Writes the dataframe df to a comma separated value
file
save(df, file=‘file.Rdata’) Writes Rdata to a file

2. Standard Input and Output Stream

Command Action
a<-readline(‘Enter age’) Reads input from the screen
b<-as.integer(a) Converts character to integer
STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data

Most materials are from reference books. For use in lectures only. Not for duplication Page | 21
print(paste(“My age is”,a)) Combines “My age is” and a , then print to screen
grep(pattern,x) Find regular matches pattern in x
gsub(pattern,x) Replace matches in x with a string pattern
toupper(x) Converts character to uppercase
tolower(x) Converts character to lower

Example 1.4 Basic Input and Output

R can read and write to .txt and .csv files. Open notepad, type A,B,C enter 1,2,3, enter 4,5,6
enter , and the cursor should appear in a new line. Then save the file as data.csv.

In the following R program, we shall practise how to read and write .txt and .csv files in R.
Take a look in the following example:

1. # Example 1.4
2. df<- read.csv(‘data.csv’)
3. rolnames(df)<-c(“Hi”,”Low”)
4. write(df,’data2.csv’)
5. a<- readline(‘Enter age:’) # Example 1.4 prompt user for an age in string format
6. b<- as.integer(a) # Converts a into integer
7. print(paste(“My age is”,a)) # Combines “My age is” and a , then print to screen


Tutorial Assignment 1.4 Basic Input and Output

1. Create a .csv file with two rows and four columns of numbers. Label the columns with
A,B,C,D. Save the file as data.csv.
2. Copy the data.csv to your current working directory.
3. Read the file data.csv in R and save as a dataframe called df.
4. Write the dataframe df to a delimited txt file called data.txt.
5. Prompt a user to enter an integer value as follow:
Enter a number:
6. Save the number to an integer and store it as b.
7. Write R code such that if the user input value is 5, then the program will display:
The integer you have just entered is: 5
8. Save the file as -Ex_11.R. For example, if your ID
is 11223344 and your name is Chan Tai Man, then save the file as 11223344-
ChanTaiMan-Ex_1_4.R
STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data

Most materials are from reference books. For use in lectures only. Not for duplication Page | 22
Conditionals. loops and Functions

When we’re programming in R (or any other language, for that matter), we often want to control
when and how particular parts of our code are executed. We can do that using control structures
like if-else statements, for loops, and while loops. Control structures are blocks of code that
determine how other sections of code are executed based on specified parameters.

One of the great strengths of R is the user's ability to add functions. To define a new function, refer
to the following table below:

1. For Loop, While Loop, if statements and functions



2. Relational and Logical Operators

Command Action
Relational Operators
a < b a is smaller than b
a > b a is larger than b
a == b a is equal to b
a <=b a is smaller or equal to b
STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data

Most materials are from reference books. For use in lectures only. Not for duplication Page | 23
a >= b a is larger or equal to b
a != b a is not equal to b
Logical Operators
(a==5) && (b==7) Testing whether the first and second conditions are both true.
If a and b are vectors, only the first element of each vector is examined.
(a==5) || (b==7) Testing either the first or second condition is true
If a and b are vectors, only the first element of each vector is examined
a&b Elementwise logical AND operation.
a|b Elementwise logical OR operation.
!(a > 5) Testing if the opposite of (a>5) is True.

Example 1.5 Conditionals, loops and Functions

In the following R program, we shall practise how to write a program that chooses among
alternative statement(s) to execute, use if…else structure. For example, the above program
saved displays "Passed" when the user enters 60 or above. It displays nothing if the user enters
any value below 60. If you want to display the word "Failed" when the user enters any value
below 60 in that program, then if…else structure works for you.

If condition
1. # Example 1.5.1 (if condition)
2. a<- readline(‘Enter a score:’) # Example 1.4 prompt user for an age in string format
3. score<- as.integer(a) # Converts a into integer
4. if (score >=60){
5. print(“Passed”)
6. } else {
7. print(“Failed”)
8. }

For loop
1. # Example 1.5.2 (for loop)
2. for (a in c(“hi”,”mid”,”low)){ # loops over a list with three elements “hi”,”mid”,”low”
3. print(a)
4. }

Function
1. # Example 1.5.3
2. grade <- function(score){
3. if (score >=80){
4. grade<- ‘A’
5. } else if (score >=40) {
6. grade<-‘P’
7. } else {
8. grade <-‘F’
9. }
10. return(grade)
11. }
12. grade(90)


STAT S263F Big Data Analytics and Applications Tutorial 1: Introduction to Big Data

Most materials are from reference books. For use in lectures only. Not for duplication Page | 24
Tutorial Assignment 1.5 Conditionals, loops and Functions

1. Write a R program that prompts for user input of score with the following
messages:
Enter the score:
A staff member gets a “pass” if both the score is at least 50. Otherwise, the staff
member obtains a “fail”. Suppose a user enters 75 for coursework marks, the program
gives the following output:

Enter the score: 75
Your score is 75 (Pass)

Suppose the user enters 40 for the score:

Enter the score: 40
Your overall result is 40 (Fail)

2. Write a R program using a for loop to print the following:

Anson Lo is a member of Mirror. He was born in 1988.
Keung To is a member of Mirror. He was born in 1989.
Edan Lui is a member of Mirror. He was born in 1997
Jer Lau is a member of Mirror. He was born in 1992.
Frankie Chan is a member of Mirror. He was born in 1989.
Ian Chan is a member of Mirror. He was born in 1990.

3. Write an R function to calculate age for each of the Mirror members in Q2.

4. Sort the members according to their age in ascending order to print the following:

Edan Lui is a member of Mirror. He is 24 years old.
Jer Lau is a member of Mirror. He is 29 years old.
Ian Chan is a member of Mirror. He is 31 years old.
Keung To is a member of Mirror. He is 32 years old.
Frankie Chan is a member of Mirror. He is 32 years old.
Anson Lo is a member of Mirror. He is 33 years old.

5. Save the file as -Ex_1_5.R. For example, if your ID is
11223344 and your name is Chan Tai Man, then save the file as 11223344-
ChanTaiMan-Ex_1_5.R


-END-

Reference:

[1] Prajapati, Vignesh. Big data analytics with R and Hadoop. Packt Publishing Ltd, 2013.

[2] Crawley, Michael J. The R book. John Wiley & Sons, 2012.



学霸联盟


essay、essay代写