Due: Sunday, 08-Sept. at 8pm

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 (and then with nobody else except the course instructors). 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.

Assignment:

1. Before going forward, read the Course Syllabus as well as the Getting Started and Programming Basics lessons. [5 pts]

2. Open RStudio and create a new project called “hw1-lastName”, replacing “lastName” with your last name.

3. Create a new R script and save it as “hw1.R” in the R project folder you just created.

4. 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:

# hw1.R
# Name:      Last, First
# GW Net ID: your_GWNetID_here

# I worked with the following classmates on this assignment:

# 1) Name: Last, First
# 2) Name: Last, First
1. Type all of your answers to the following questions in the “hw1.R” script. [point breakdown is listed by each problem]

2. After completing the questions, create a zip file of all files in your R project folder for this assignment and submit the zip file on Blackboard by the due deadline (note: to receive full credit, your submission must follow the above format of using a correctly-named R Project and .R script). [5 pts]

## Questions to answer in “hw1.R” script

### 1) Reasoning over code [SOLO, 10 pts]

Consider the following objects:

w <- TRUE
x <- FALSE
y <- FALSE
z <- TRUE

Write code to answer the following questions:

1. Write a logical statement that compares the objects x, y, and z and returns TRUE
2. Write a logical statement that compares the objects x, y, and z and returns FALSE
3. Fill in the correct relational operators to make this statement return TRUE: ! (x __ y) & ! (z __ y)
4. Fill in the correct relational operators to make this statement return FALSE: ! (w __ y) | (z __ y)

### 2) Objects [SOLO, 10 pts]

Create objects to store each of the following values. Use names that make sense and are easy to understand:

• The number of seconds in a minute
• The number of minutes in an hour
• The number of hours in a day
• The number of days in a typical year (not a leap year)

Use the objects you created to calculate the following (you may create other, intermediate objects, but you may not use numbers in your solution):

1. The number of seconds in a day
2. The number of seconds in a year
3. The number of minutes in a day
4. The number of minutes in a year
5. The number of hours in a year

### 3) Character data [SOLO, 5 pts]

Load the following string in R:

sentence <- 'The quick Brown fox Jumped over the Lazy dog'

Install and load the stringr library, then use the str_to_lower(), str_to_upper(), and str_to_title() functions and the cat() function to print out the following variations of the object sentence that you just created (hint: you may want to first look up what the str_to_lower(), str_to_upper(), and str_to_title() functions do):

"the quick brown fox jumped over the lazy dog"
"THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG"
"The Quick Brown Fox Jumped Over The Lazy Dog"

### 4) Numeric data 1 [COLLABORATIVE, 15 pts]

Max is looking to purchase a car and is deciding between a Toyota Prius and a Toyota Camry. He knows that based on his driving patterns, he can get an average fuel economy of 55 miles per gallon (mpg) of gasoline with the Prius, which sells for $27,600. He is also considering the cheaper Toyota Camry, which costs$24,000 but only gets 32 mpg on average. He knows he typically drives 15,000 miles each year. For the following questions, assume the price of gas will remain constant at \$3.00 / gallon forever.

1. Create objects for the price and fuel economy of each vehicle as well as for the total miles Max drives each year and the price of gasoline. Use names that make sense and are easy to understand.
2. Use the objects you created to compute how many years Max would need to drive the Prius such that the money he would save in lower fuel costs overcomes the Prius’s higher upfront price. You may create other, intermediate objects in solving this, but you may not use any numbers. Ignore inflation and discount rates.
3. What would the price of gasoline need to be such that the answer to part b) is just 2 years? You may use the number 2 once in your solution, otherwise you may only use the objects you defined in parts a) and b).

### 5) Numeric data 2 [COLLABORATIVE, 15 pts]

x1 <- 1
y1 <- 1
x2 <- 5
y2 <- 1
x3 <- 5
y3 <- 4
1. Compute the Euclidean distance between the points (x1, y1) and (x2, y2) and store this object as d1.
2. Compute the Euclidean distance between the points (x2, y2) and (x3, y3) and store this object as d2.
3. Compute the Euclidean distance between the points (x1, y1) and (x3, y3) and store this object as d3.
4. Is the triangle formed by the points (x1, y1), (x2, y2), and (x3, y3) a right triangle? If so, prove it using the objects d1, d2, and d3.

### 6) Logical data [COLLABORATIVE, 15 pts]

Suppose we fielded a survey that asked respondents to specify their marital status. The responses were recorded as one of the following:

• "single"
• "widowed"
• "married"
• "de facto"
• "missing"

where "missing" means the respondent left the question blank.

Assume there is an object in your R environment called marital_status that contains one of the above responses stored as a character. For each of the following, write a single line of code that uses the marital_status object to create the desired new object (note that if you actually run your code in R for this exercise, you’ll get an error, because the marital_status object doesn’t actually exist in your R environment):

1. is_single: Is TRUE if the participant is single and FALSE otherwise.
2. has_spouse: Is TRUE if the participant is married or in a de facto relationship and FALSE otherwise.
3. not_single_or_missing: Is TRUE if the participant did respond to the question, but is not single.

### 7) Packages [COLLABORATIVE, 20 pts]

1. Read about the TurtleGraphics package HERE
2. Install the package TurtleGraphics
3. Load the TurtleGraphics library, then use the following command: turtle_init(). What happens?
4. Read the help information about the TurtleGraphics package:
help(package='TurtleGraphics')
1. Now read the help information about the turtle_move() function in the TurtleGraphics package:
?turtle_move()
1. Read through the following commands - write down what do you think is going to happen.
turtle_init()
turtle_move(distance = 10, direction = "forward")
turtle_move(distance = 20, direction = "backward")
turtle_reset()
turtle_turn(angle = 90, direction = "right")
turtle_move(distance = 10, direction = "forward")
turtle_move(distance = 20, direction = "backward")
turtle_reset()
1. Now run the above commands - what happened?
2. Using what you’ve learned about how the TurtleGraphics package works, write code to produce the following image: EMSE 6574, Sec. 11: Programming for Analytics (Fall 2019)
George Washington University | School of Engineering & Applied Science
Dr. John Paul Helveston | jph@gwu.edu | Mondays | 6:10–8:40 PM | Phillips Hall 108 | |