0% found this document useful (0 votes)
3 views23 pages

Lab 08

Lab 08 for ENGG1004 focuses on analyzing student scores using R, requiring students to write a script that calculates mean, standard deviation, best, and worst scores from user input. Students must follow a structured approach to coding, including preparing variables, looping through inputs, and analyzing results. A bonus task involves determining which student has the highest average score, with submissions required via Blackboard.

Uploaded by

cho
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)
3 views23 pages

Lab 08

Lab 08 for ENGG1004 focuses on analyzing student scores using R, requiring students to write a script that calculates mean, standard deviation, best, and worst scores from user input. Students must follow a structured approach to coding, including preparing variables, looping through inputs, and analyzing results. A bonus task involves determining which student has the highest average score, with submissions required via Blackboard.

Uploaded by

cho
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
You are on page 1/ 23

ENGG1004

Digital Literacy and


Computational Thinking—R
Lab 08 R Coding Again
2024-25 Term 2
Lab 08 Tasks

 Study a problem
 Prepare the input
 Loop through all input
 Build an inner loop to solve the problem
 Analyze the solution
 Upload and submit your saved file to Blackboard
 Bonus

2
Problem:
Analyze Student Scores

 Write an R script to calculate the mean,


standard deviation, best score, and worst score
of a set of course scores provided by the user.

Course # Scores Mean 85


Course 1 100 SD 12.2474487
Course 2 70 Best 100
Course 3 85 Worst 70

3
Population standard deviation

A simple way to calculate:


2
Σ(𝑥 2 ) Σ 𝑥

𝑛 𝑛
4
Problem:
Analyze Student Scores

 Download the template from Blackboard

 Rename the file from lab08_template.R to


lab08.R

 Still remember how to open the RStudio and


write our own R code?
5
Recap: Save Your Work Properly

 Setup your own filing system:


 E.g., create a folder named ENGG1004 for your works in
this course
 Keep on your own computer, e.g., Documents
 Keep on portable storage such as USB drive
 Keep on cloud storage such as OneDrive (CUHK O365)

 Folder structure is hierarchical, i.e., tree-like


with branches called sub-folders

6
Code the Solution in RStudio
 Open RStudio in your computer
 Menu File > Open File

 Choose the folder and file you saved from


Blackboard

7
Template Code

8
Prepare the variables
 Input the number of total courses

 Define four variables, please initialize them with


correct values accordingly
 Change all the ...
 Hints: Before we start, what should their initial values be?
9
Loop for every course

 Use a for loop!

 Variable i iterates for each


course
 E.g. i = 1,2,3,4,5,6 when
the course number is 6
 What is the range of i ?
 Fill in here

10
Input all the course scores
 Variable i iterates for each
course
 E.g. i = 1,2,3,4,5,6 when the
course number is 6
 Want to input scores for the
course i
Sample input/output

11
Input all the course scores
 Variable i iterates for each
course
 E.g. i = 1,2,3,4,5,6 when the
course number is 6
 Get input scores for the course i
Sample input/output

12
Sum all the course scores
 Variable i iterates for each
course
 E.g. i = 1,2,3,4,5,6 when the
course number is 6

 Sum the course score iteratively


 𝑆𝑢𝑚1 𝑡𝑜 𝑖 = 𝑆𝑢𝑚1 𝑡𝑜 (𝑖−1) + 𝑠𝑐𝑜𝑟𝑒𝑠𝑖
 Replace the …

13
Find the Best and Worst
 Variable i iterates for each
course
 E.g. i = 1,2,3,4,5,6 when the
course number is 6

 Using if statement
 If we get a larger score, update
the best
 If we get a lower score, update the
worst
 Replace the …

14
Calculate the mean and SD
 Replace …

A simple way to calculate SD:


2
Σ(𝑥 2 ) Σ 𝑥

𝑛 𝑛

15
Whenever scan() is used,
Run the Program choose Source but not Run

 Click Source

 Input the values and test your


code on the Console
 Input :
 3 (Enter)
 99 (Enter)
 99 (Enter)
 99 (Enter)

16
Try different inputs
 Every time you “source”, you can type a different
input to get a different output!

17
Blackboard Submission

 Login Blackboard course ENGG1004

 Go to Labs > Lab 08: R Coding Again

 Under Attach Files, Browse Local Files > select


your saved file lab08.R

 Click Submit

18
Academic Honesty and Declaration
Statement
 You must read the University Guideline on Academic Honesty
(https://www.cuhk.edu.hk/policy/academichonesty/), and place the following declaration
statement (with your information filled in) at the end of your .R script file as comments

 You should NOT share your file to others, regardless of your intention

 You should NOT obtain other’s works by any means

 Submitting the wrong file by “accident” will NOT be accepted as an excuse – so please
double-check which file you have submitted
# Declaration

# I declare that the lab work here submitted is original


# except for source material explicitly acknowledged,
# and that the same or closely related material has not been
# previously submitted for another course.
# I also acknowledge that I am aware of University policy and
# regulations on honesty in academic work, and of the disciplinary
# guidelines and procedures applicable to breaches of such
# policy and regulations, as contained in the website.
#
# University Guideline on Academic Honesty:
# https://www.cuhk.edu.hk/policy/academichonesty/

# Student Name : <your name>


# Student ID : <your student ID>
# Class/Section : <your class/section>
# Date : <date> 19
Bonus
 Now, there could be more than one student.

 Please new an empty file, and rename it as lab08-


bonus.R

Now, we want to know who gets the highest average scores


20
Bonus
 Please new an empty file, and rename it as lab08-
bonus.R
 Now, we want to know who gets the highest average
scores
 Input the number of students
 For each student calculate the mean
 Compare with current best_mean, and update it
 Also need to remember the best student id

 You can copy the code from lab08.R


 Edit it into the bonus
 Delete some useless lines and add more
21
Bonus Examples
 Who gets the highest average scores

 If you attempt the bonus, submit two files together


lab08.R and lab08-bonus.R
22
Blackboard Submission

 Login Blackboard course ENGG1004

 Go to Labs > Lab 08: R Coding Again

 Under Attach Files, Browse Local Files > select


your saved file lab08.R (and lab08-bonus.R)

 Click Submit (You can resubmit several times, we


only grade your last submission)

23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy