**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**:

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

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

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

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
```

Type all of your answers to the following questions in the “hw1.R” script. [point breakdown is listed by each problem]

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]

Consider the following objects:

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

Write code to answer the following questions:

- Write a logical statement that compares the objects
`x`

,`y`

, and`z`

and returns`TRUE`

- Write a logical statement that compares the objects
`x`

,`y`

, and`z`

and returns`FALSE`

- Fill in the correct relational operators to make this statement return
`TRUE`

:`! (x __ y) & ! (z __ y)`

- Fill in the correct relational operators to make this statement return
`FALSE`

:`! (w __ y) | (z __ y)`

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):

- The number of seconds in a day
- The number of seconds in a year
- The number of minutes in a day
- The number of minutes in a year
- The number of hours in a year

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"
```

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.

- 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.
- 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.
- 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).

Load the following objects into R, then answer the questions below:

```
x1 <- 1
y1 <- 1
x2 <- 5
y2 <- 1
x3 <- 5
y3 <- 4
```

- Compute the Euclidean distance between the points (x1, y1) and (x2, y2) and store this object as
`d1`

. - Compute the Euclidean distance between the points (x2, y2) and (x3, y3) and store this object as
`d2`

. - Compute the Euclidean distance between the points (x1, y1) and (x3, y3) and store this object as
`d3`

. - 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`

.

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):

`is_single`

: Is`TRUE`

if the participant is single and`FALSE`

otherwise.`has_spouse`

: Is`TRUE`

if the participant is married or in a de facto relationship and`FALSE`

otherwise.`not_single_or_missing`

: Is`TRUE`

if the participant did respond to the question, but is not single.

- Read about the
`TurtleGraphics`

package HERE - Install the package
`TurtleGraphics`

- Load the
`TurtleGraphics`

library, then use the following command:`turtle_init()`

. What happens? - Read the help information about the
`TurtleGraphics`

package:

`help(package='TurtleGraphics')`

- Now read the help information about the
`turtle_move()`

function in the`TurtleGraphics`

package:

`?turtle_move()`

- 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()
```

- Now run the above commands - what happened?
- Using what you’ve learned about how the
`TurtleGraphics`

package works, write code to produce the following image:

George Washington University | School of Engineering & Applied Science

Dr. John Paul Helveston | jph@gwu.edu | Mondays | 6:10–8:40 PM | Phillips Hall 108 | |

This work is licensed under a Creative Commons Attribution 4.0 International License.

See the licensing page for more details about copyright information.

Content 2019 John Paul Helveston