CS计算机代考程序代写 —


title: “Practice Exam Part 2 – Solution”
author & honor code compliance: “
date: “
output: html_document

“`{r setup, include=FALSE}
# Chunk 1. Generated knit setting
knitr::opts_chunk$set(echo = TRUE)
“`

## R Markdown

Reminder: This is a Practice Exam Part 2 for the R Proficiency Exam Part 2.
Goals:
1. Prepare for the R Proficiency Exam

Tips for the exam:
1. Your Name and NetID in line 3 will indicate complaince with Fuqua Honor Code (https://www.fuqua.duke.edu/honorcode) for the final exam. You must submit with that to received credit for this exam.
2. The exam is open book/internet/notes/etc. However, you can’t discuss with any living being except Dr. Salman Azhar.
3. The final exam part 2 will use a dataset similar to nycflights in this practice. Please review https://cran.r-project.org/web/packages/nycflights13/nycflights13.pdf to understand the structure of the dataset before the exam. While the data will be slightly different, the structure will be identical.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.

*Note: Numbering corresponds to chunk numbers. Chunk #1 specified knitting*
*2. Read nycflightsID.csv into a dataframe flights*
*Tip: check the structure of flights to verify that the file.*
“`{r}

“`

*3. Now, let’s analyze distance & air_time column of flights by plotting it. Add a trendline using smooth (or another function that adds a trendline).*
*Hint: Use qplot or any other plotting function with distance = y-axis & air_time as x-axis.*
*Tip: If you update the ggplot2 package without updating the scales package, you will get a cryptic error message. install.packages(“scales”) can fix this error. Make sure that ggplot is installed and running before your exam. *
“`{r}

“`

*4. Compute a vector called speed, based on the total distance divided by the total air time (distance/air_time), and add this column to flights. *
*Also, print the structure of flights and statistical summary of flights’s speed column to verify that your code worked.*
*Tip: Your answer should be in miles/hour.*
“`{r}

“`

*5. Print the mean and sd for the vector flights$speed – Your answers should be in miles/hour*
*Tip: If you get an error, review the warning in the qplot to check if there are any missing values (that you should ignore).*
“`{r}

“`
*6. Print the mean and standard deviation of speed for American, Delta, and United*
*Your answers should be in miles/hour*
*Tip: The airline is encoded in carrier column as AA (American), DL (Delta), UA (United), etc.*
“`{r}

“`

*7. Write a function speedAirline that give the average speed of all flights for an airline that is passed as a parameter to speedAirline*
*Tip: Check your function for AA, DL, and UA.*
“`{r}

“`

*8. Check your function speedAirline for AA, DL, and UA. Fix any errors if they don’t match with your earlier results.*
*Tip: It pays to double check your work. You should do that even if you are not told to do that.*
“`{r}

“`

*9. Using your function speedAirline inside a for-loop, compute and print the airline with the slowest average speed based on your data.*
*Hint: unique(flights$carrier) will give you the unique values for your carriers.*
“`{r}

“`

*10. Now, compute and print a dataframe named airlineSpeeds. The dataframe airlineSpeeds has all airline carrier codes in the first column and the average speed of each airline in the second column.*
*You must do this in 2-3 lines*
*Hint: unique(flights$carrier) will give you the unique values for your carriers. The apply family will enable you to do this in 2-3 lines.*
“`{r}

“`

*11. Now, find and print the fastest and the slowest airline speed*
*Tip: Check your answer to make sure it is consistent with previous answers.*
“`{r}

“`

*12. What is the type of your results above and does it make sense?*
*Hint: No code required as you can draw this conclusion from your previous answers.*
“`{r}

“`

*13. Fix the type of airlineSpeed.*
*Hint: Google “converting chr into number in r” or something like that*
“`{r}

“`

*14. Knit to html after eliminating all the errors. Do not worry about minor formatting issues.* *Tip: This will take some time as you are processing medium size data sets.*