-
Notifications
You must be signed in to change notification settings - Fork 1
/
hw1-programming-basics.Rmd
170 lines (123 loc) · 7.79 KB
/
hw1-programming-basics.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
---
title: "Homework 1 - Programming Basics"
output:
html_document:
number_sections: false
toc: no
---
**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](#syllabus.html) as well as the [Getting Started](L1.2-getting-started.html) and [Programming Basics](L2-programming-basics.html) 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:
```{r eval=FALSE}
# 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
```
5) Type all of your answers to the following questions in the "hw1.R" script. [point breakdown is listed by each problem]
6) 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:
```{r}
w <- TRUE
x <- FALSE
y <- FALSE
z <- TRUE
```
Write code to answer the following questions:
a) Write a logical statement that compares the objects `x`, `y`, and `z` and returns `TRUE`
b) Write a logical statement that compares the objects `x`, `y`, and `z` and returns `FALSE`
c) Fill in the correct relational operators to make this statement return `TRUE`: `! (x __ y) & ! (z __ y)`
d) 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):
a) The number of seconds in a day
b) The number of seconds in a year
c) The number of minutes in a day
d) The number of minutes in a year
e) The number of hours in a year
### 3) Character data [SOLO, 5 pts]
Load the following string in R:
```{r eval=FALSE}
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):
```{r eval=FALSE}
"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.
a) 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.
b) 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.
c) 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]
Load the following objects into R, then answer the questions below:
```{r eval=FALSE}
x1 <- 1
y1 <- 1
x2 <- 5
y2 <- 1
x3 <- 5
y3 <- 4
```
a) Compute the [Euclidean distance](https://en.wikipedia.org/wiki/Euclidean_distance) between the points (x1, y1) and (x2, y2) and store this object as `d1`.
b) Compute the Euclidean distance between the points (x2, y2) and (x3, y3) and store this object as `d2`.
c) Compute the Euclidean distance between the points (x1, y1) and (x3, y3) and store this object as `d3`.
d) Is the triangle formed by the points (x1, y1), (x2, y2), and (x3, y3) a [right triangle](https://en.wikipedia.org/wiki/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):
a) `is_single`: Is `TRUE` if the participant is single and `FALSE` otherwise.
b) `has_spouse`: Is `TRUE` if the participant is married or in a de facto relationship and `FALSE` otherwise.
c) `not_single_or_missing`: Is `TRUE` if the participant did respond to the question, but is not single.
### 7) Packages [COLLABORATIVE, 20 pts]
a) Read about the `TurtleGraphics` package [HERE](TurtleGraphics.html)
b) Install the package `TurtleGraphics`
c) Load the `TurtleGraphics` library, then use the following command: `turtle_init()`. What happens?
d) Read the help information about the `TurtleGraphics` package:
```{r eval=FALSE}
help(package='TurtleGraphics')
```
e) Now read the help information about the `turtle_move()` function in the `TurtleGraphics` package:
```{r eval=FALSE}
?turtle_move()
```
f) Read through the following commands - write down what do you think is going to happen.
```{r eval=FALSE}
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()
```
g) Now run the above commands - what happened?
h) Using what you've learned about how the `TurtleGraphics` package works, write code to produce the following image:
![](images/turtle_hw1.png){ width=456 }