Project 5 Assignment
Score is 30 points (10 poinst each question).
Question 1
Exploring and Analyzing Data Using R Functions
Objective
This assignment will assess your ability to:
• Import and explore a dataset in R
• Apply basic R functions for data manipulation and analysis
• Generate summary statistics and visualizations
• Write clean, well-commented R code
Instructions
1. Dataset Selection
Use the built-in dataset mtcars in R for this assignment.
2. Tasks
(A) Data Exploration
o Display the first 6 rows of the dataset using an appropriate R function.
o Use str(), summary(), and dim() functions to explore the dataset.
o Identify the variable with the highest mean value using R code.
(B) Data Manipulation
o Create a new variable efficiency as mpg/hp. Add this as a new column.
o Filter the dataset to include only cars with mpg > 20.
o Sort the dataset in descending order based on efficiency.
(C) Data Analysis
o Calculate the mean, median, and standard deviation of mpg, hp, and wt.
o Use the aggregate() function to compute the average horsepower (hp)
grouped by the number of cylinders (cyl).
o Create a bar plot to compare the average mpg by cylinder count.
(D) Code Presentation (20 marks)
o Your R script should include meaningful comments.
o Use clear, readable formatting.
o Include a brief summary (3–5 sentences) of your findings in a comment block
at the top.
Question 2
R Programming Assignment: User-Defined Functions
Assignment: Analyzing Student Grades
Scenario: You've been given a dataset of student scores for three different assignments
(Assignment 1, Assignment 2, and Final Exam). You need to create R functions to perform
some basic analysis on these scores.
Instructions:
1. Create a function calculate_average_score:
o This function should take a numeric vector of scores as input.
o It should return the average (mean) of these scores.
o Handle the case where the input vector might contain NA values by removing
them before calculation.
2. Create a function assign_grade_category:
o This function should take a single numeric score (representing an average
score) as input.
o It should return a character string representing the grade category based on
the following criteria:
▪ 90-100: "Excellent"
▪ 80-89: "Good"
▪ 70-79: "Satisfactory"
▪ 60-69: "Pass"
▪ Below 60: "Fail"
o Assume the input score will be between 0 and 100.
3. Demonstrate your functions:
o Create a sample data frame named student_scores with the following data:
Student Assignment1 Assignment2 FinalExam
Alice 85 92 88
Bob 70 NA 75
Carol 95 88 90
David 55 60 50
o Export to Sheets
o Using your calculate_average_score function, add a new column to
student_scores called OverallAverage which contains the average of
'Assignment1', 'Assignment2', and 'FinalExam' for each student.
o Using your assign_grade_category function, add another new column to
student_scores called GradeCategory based on the OverallAverage score.
o Print the final student_scores data frame.
Question 3
R Assignment: Calculating the Area of a Circle
Objective: Create a user-defined function in R that calculates the area of a circle given its
radius.
Instructions:
1. Define a function named calculate_circle_area that takes one argument: radius.
2. Inside the function, use the formula for the area of a circle: Area=pi\*radius2.
3. The function should return the calculated area.
4. Test your function by calculating the area of a circle with a radius of 5 and another
with a radius of 10.
5. Print the results clearly.
Submission Guidelines
• Submit an .R script file named lastname_firstname_assignment.R.
• Make sure your code runs without errors.
• Use only base R and built-in datasets/functions (no external libraries).
版权所有:留学生编程辅导网 2020 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。