Due: 14 September by 11:00 pm
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.
- 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.
- Even though you work collaboratively, you still must submit your own solutions.
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 Net ID, and the names of anyone you worked with on this assignment:
# Name: Last, First # GW Net ID: Insert your GWNetID 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.
Consider the points A, B, and C defined by the following cartesian coordinates:
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:
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 $2.20 / 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:
To solve each problem, apply Polya’s problem solving technique:
In your hw2.R file, create two objects that store the following character values:
Use meaningful variable names to store these values.
Next, install the stringr library. Try using the
str_to_title() functions from the stringr library 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)
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 devtools library. Then you can use the
devtools::install_github() function to install packages directly from GitHub. To try this out, install the devtools library, then trying installing the BRRR package:
Note: Packges on GitHub are in development and often require other packges 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
#hw2 channel on slack, post your favorite argument to
skrrrahh() (mine is 24).
In case you missed the intro to the TurtleGraphics library in class, go ahead and install and load the library. You might also want to review the related lesson page and the class slides. Then complete these tasks:
TurtleGraphicslibrary, then initiate your turtle with the
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()
Create a zip file of all the files in your R project folder for this assignment and submit the zip file on Blackboard (note: to receive full credit, your submission must follow the above format of using a correctly-named R Project and