Homework 2 - Functions & Packages
Due: Feb 01 by 11:59pm
Submission Instructions: Create a zip file of all the files in your R project folder for this assignment, then submit your zip file on the corresponding assignment submission on Blackboard.
Weight: This assignment is worth 4% of your final grade.
Purpose: The purposes of this assignment are:
- To practice using functions in R.
- To practice converting data types in R.
- To practice installing, loading, and using external packages.
Skills & Knowledge: After completing these exercises, you should be able to:
- Be familiar with how to specify arguments in functions.
- Be comfortable converting data between logical, character, and numeric types.
- Be comfortable installing external libraries and accessing functions in them.
Assessment: Each question indicates the % of the assignment grade, summing to 100%. The credit for each question will be assigned as follows:
- 0% for not attempting a response.
- 50% for attempting the question but with major errors.
- 75% for attempting the question but with minor errors.
- 100% for correctly answering the question.
The reflection portion is always worth 10% and graded for completion.
Rules:
- Problems marked SOLO may not be worked on with other classmates, though you may consult instructors for help.
- For problems marked COLLABORATIVE, you may work in groups of up to 3 students who are in this course this semester. You may not split up the work – everyone must work on every problem. And you may not simply copy any code but rather truly work together and submit your own solutions.
Readings
The readings from the last week will serve as a helpful reference as you complete this assignment. You can review them here:
1) Staying organized [SOLO, 10%]
Just like we did in HW1, we’re going to use an R project to keep everything in this assignment tidy. Create a new R project called “hw2” and save a new R script in it called “hw2.R”. Copy the following code to the top of this script and fill out your name, GW netID, and the names of anyone you worked with on this assignment:
# Name: Last, First
# netID: Insert your netID here
# I worked with the following classmates on this assignment:
# 1) Name: Last, First
# 2) Name: Last, First
Write your responses to all other questions in this assignment in your hw2.R file.
2) Polya problem solving 1 [COLLABORATIVE, 15%]
Consider the points A, B, and C defined by the following cartesian coordinates:
Point | x |
y |
---|---|---|
A | 1 | 1 |
B | 5 | 1 |
C | 5 | 4 |
Your task is to find out if the triangle formed by the points A, B, and C is a right triangle. Apply Polya’s problem solving technique:
- Understand the problem: In a code comment, write the question you are trying to solve in your own words, then create objects for each relevant piece of information provided in the problem statement.
- Devise a plan: In code comments, describe the steps you will take in your plan to solve the problem.
- Carry out the plan: Write code below your comments to carry out your plan.
- Check your work: Examine your solution and any intermediate values you computed along the way. Do the values make sense (e.g. do you get negative numbers when you expected positive numbers)? Is there another way you could have solved it to check that you get the same result?
3) Polya problem solving 2 [COLLABORATIVE, 20%]
Kevin is deciding between purchasing a Chevy Bolt, which sells for $29,390 (after subsidies), and a Toyota Camry, which sells for $24,000. The Bolt efficiency is 28 kWh of electricity per 100 miles while the Camry efficiency is 28 miles per gallon (mpg) on average. The average price of electricity (in DC, where Kevin lives) is $0.13 / kWh, and the average price of gasoline is $3.00 / gallon. Kevin drives 12,000 miles each year on average.
To help him make a decision about which car to buy, answer the following questions:
- How long (in years) would he have to drive the Bolt for the money he saves in fuel savings to be greater than the price premium compared to the Camry?
- What would the price of gasoline need to be such that the fuel savings from driving the Bolt for 3 years would be greater than the price premium compared to the Camry?
- How many miles would Kevin need to drive each year such that the fuel savings from driving the Bolt for 3 years would be greater than the price premium compared to the Camry?
To solve each problem, apply Polya’s problem solving technique:
- Understand the problem: In a code comment, write the question you are trying to solve in your own words, then create objects for each relevant piece of information provided in the problem statement.
- Devise a plan: In code comments, describe the steps you will take in your plan to solve the problem.
- Carry out the plan: Write code below your comments to carry out your plan.
- Check your work: Examine your solution and any intermediate values you computed along the way. Do the values make sense (e.g. do you get negative numbers when you expected positive numbers)? Is there another way you could have solved it to check that you get the same result?
4) Working with strings: the stringr package [SOLO, 15%]
Who is your favorite super hero character? If you don’t already have one, pick one (mine is Professor Xavier, and no it’s not just because he’s a professor :P).
In your hw2.R file, create two objects that store the following character values:
- The name of your favorite super hero.
- The super power(s) your hero has.
Use meaningful variable names to store these values.
Next, install the stringr package. Try using the str_to_lower()
, str_to_upper()
, and str_to_title()
functions from the stringr package on the objects you created. In a code comment, describe what these functions do.
Finally, use the paste()
function to merge your super hero’s name and power in a coherent sentence. For example, if the name is "Professor Xavier"
and the the power is "reads peoples' minds"
, then a final sentence might be: "Professor Xavier reads peoples' minds!"
. (Note that I intentionally did not tell you how to use the paste()
function…you’ll have to figure that one out)
5) Installing packages from Github: the BRRR package [SOLO, 10%]
The vast majority of the time, you will install external packages using the install.packages()
function. This installs packages from the Comprehensive R Archive Network (CRAN), where most packages are published. But you can also install packages that are under development or haven’t been published to CRAN yet. Most of the time, these packages are hosted on GitHub - an online platform for sharing code (it’s also where all of the files that make up this website are stored).
To install a package from GitHub, you first need to install the remotes package. Then you can use the remotes::install_github()
function to install packages directly from GitHub. To try this out, install the remotes package, then trying installing the BRRR package:
::install_github("brooke-watson/BRRR") remotes
Note: Packages on GitHub are in development and often require other packages to work. So if you get an installation error about some other package dependency, try restarting your R session and try again.
Not sure what this package does? Well, one of the other nice things about packages listed on GitHub is the authors tend to write detailed descriptions - check out the GitHub page for the BRRR package. Then try using the BRRR::skrrrahh()
function with different number arguments (turn your volume up). In the #hw
channel on slack, post your favorite argument to skrrrahh()
(mine is 24).
6) Turtle practice [COLLABORATIVE, 20%]
In case you missed the intro to the TurtleGraphics package in class, go ahead and install and load the package. You might also want to review the related lesson page and the class slides. Then complete these tasks:
- Load the TurtleGraphics package, then initiate your turtle with the
turtle_init()
function. - Read through the following commands (don’t run them). In a code comment, type a description of what you think the turtle is going to do.
turtle_init()
turtle_move(distance = 25, direction = "forward")
turtle_move(distance = 50, direction = "backward")
turtle_reset()
turtle_turn(angle = 90, direction = "right")
turtle_move(distance = 25, direction = "forward")
turtle_move(distance = 50, direction = "backward")
turtle_reset()
- Now run the above commands to check if the turtle did what you expected. Congratulations, you’ve just practiced “code tracing”! Reading through code before running it then comparing the result with your expectation is an excellent method for improving your your computational thinking skills and your fluency with a particular language.
- Using what you’ve learned about how to manipulate your turtle, write two different solutions for producing the following image (hint: the length of one side of the square is
50
units):
7) Read and reflect [SOLO, 10%]
Read and reflect on the following readings to preview what we will be covering next:
Afterwards, in a comment (#
) in your .R file, write a short reflection on what you’ve learned and any questions or points of confusion you have about what we’ve covered thus far. This can just few a few sentences related to this assignment, next week’s readings, things going on in the world that remind you something from class, etc. If there’s anything that jumped out at you, write it down.
Submit
Instructions for how to submit your assignment are at the top of this page.