You may (in fact, are encouraged to) use the Internet to search up any information or example code to help you with this assignment, though you must cite any external (i.e. non-course related) websites that you use. Similarly, after attempting this assignment by yourself, you may collaborate with other students in the course, but you must each write your own code and acknowledge all students with whom you collaborated for each problem (you don’t need to cite by subpart). However, you may not post on Internet forums (e.g. Stack Exchange) for help with this assignment; doing so is considered an Honor Code violation.
Please provide your responses to each problem in this .Rmd
file directly below each subpart, inserting additional R code chunks if needed. For text responses, place them in between the provided <p>
tags (this puts them in a grey-background textbox, to make them easier to grade). On Gradescope, you need only submit the .html
file created by knitting the document with your responses. Problem 0 will provide guidance on how to do this.
Read Sections 2.2-2.6 of “R Markdown: The Definitive Guide”. It will introduce you to using R markdown. You may skip subsection 2.5.3 if you are not familiar with LaTeX. It is not needed for this class.
Load the nycflights13
package.
Consider the airports
tibble. How many observations and variables does it have?
What is the mean altitude of all the airports in the tibble?
How many different time zones are represented among the airports in the tibble?
Select the name, lat, lon, and alt of the 3rd and 5th airports in the tibble. The result should be just one 2 x 4 tibble.
Write a function called percent_decrease
that takes in two arguments — num
and percent
— and returns the value when num
is decreased by percent
percent. For instance, if num
is 10 and percent
is 20, you should return 8.
Use your function from (a) to calculate the cost of an item of clothing which originally sold for $39.99, but is on sale for 40% off. Use round()
to print out the price to the nearest cent.
Define evens
below to be a vector consisting of all even integers between 0 and 2022, inclusive. Hint: look at the help menu for the seq()
function!
What does this code do? (Uncomment it first). If it doesn’t run properly, check your answer to (b).
# length(evens)
# decreased_evens <- percent_decrease(num=evens, percent=20)
Note that evens
is a numeric vector, even though you designed your function percent_decreases()
to take in a single number as its first argument. Explain how R makes sense of this.
# decreased_evens_2 <- percent_decrease(num=evens, percent=c(20,100))
Explain what’s going on now.
Karen needs to buy cookies for her daughter’s 6th birthday party. She only shops at Costco, which sells cookies in batches of 12. There will be 21 cookie-eating guests at the party.
Suppose Karen wants to give every cookie-eating guest the same number of cookies, but is very concerned about having as few left over as possible, since she personally hates cookies. Karen is very indecisive and considers buying anywhere from 2 to 10 batches, inclusive. In one line of code, create a length 9 vector called leftovers
specifying how many left over cookies she will have for each number of batches from 2 to 10. You may want to read up on the modulo operator %%
.
Convert leftovers
to a list with names 2,…,10, corresponding to the number of batches.
Run the following two lines of code. Explain what kind of data structure and data type(s) each line returns, and explain the content of what they are returning.
# leftovers < 4
# leftovers[leftovers < 4]
Use names()
and part (c) to return all numbers of batches Karen can consider to have less than 4 cookies left over.
What does the following line do?
# leftovers[-2]