-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw6-vectors.Rmd
63 lines (44 loc) · 4.03 KB
/
hw6-vectors.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
---
title: "Homework 6 - Vectors"
output:
html_document:
toc: false
number_sections: false
params:
number: 6
submit: "https://u.pcloud.com/#page=puplink&code=FVYkZvcacQveKhs7ulhNTtcgVFzfgRhSX"
purpose: |
The purposes of this assignment are:
>
> - To practice using vectors in R.
> - To practice computational problem solving with vectors.
---
```{r child = file.path("child", "setup.Rmd")}
```
```{r child = file.path("child", "hw.Rmd")}
```
> ### Using the [autograder](autograder.html)
>
> - You can check your solutions to problems 2 - 6 by logging into the [autograder](autograder.html) and uploading your `hw6.R` file.
> - **The file must be named `hw6.R` or it won't work.**
> - Your user name is your netID, and your password is inside the `readme.txt` file in the Box folder I shared with you.
### 1) Staying organized [SOLO, 5%]
Download and use [this template](templates/hw6.zip) for your assignment. Inside the "hw6" folder, open and edit the R script called "hw6.R" and fill out your name, GW Net ID, and the names of anyone you worked with on this assignment.
> ### **Writing test functions**
>
> For each of the following functions, write a test function first, and then write the function. **Your test functions will count for half of the available credit for each problem**. Think carefully about the test cases to include in your test functions.
> ### **Using good style**
>
> For this assignment, you must use good style to receive full credit. Follow the best practices described in [this style guide](http://adv-r.had.co.nz/Style.html).
### 2) `vectorFactorial(n)` [SOLO, 10%]
Write the function `vectorFactorial(n)` which computes the factorial of `n` using vectors to avoid using a loop. Hint: there are some useful functions listed on the [vectors lesson page](r6-vectors.html#Numeric_vectors) for performing operators on a numeric vector.
### 3) `nthHighestValue(n, x)` [SOLO, 15%]
Write a function to find the nth highest value in a given vector. For example, if `x` equals `c(5, 1, 3)`, then `nthHighestValue(1, x)` should return `5`, because `5` is the 1st highest value in `x`, and `nthHighestValue(2, x)` should return `3` because it's the 2nd highest value in `x`. Assume only numeric inputs, and assume that `n <= length(x)`. You may not use loops.
### 4) `dotProduct(a, b)` [COLLABORATIVE, 20%]
**Background**: the "dot product" of two vectors is the sum of the products of the corresponding terms. So the dot product of the vectors `c(1,2,3)` and `c(4,5,6)` is `(1*4) + (2*5) + (3*6)`, or `4 + 10 + 18 = 32`. With this in mind, write the function `dotProduct(a, b)`. This function takes two vectors and returns the dot product of those vectors. If the vectors are not equal length, ignore the extra elements in the longer vector. You may not use loops.
### 5) `middleValue(a)` [COLLABORATIVE, 20%]
Write the function `middleValue(a)` that takes a vector of numbers `a` and returns the value of the middle element (or the average of the two middle elements).
### 6) `rotateVector(a, n)` [COLLABORATIVE, 20%]
Write the function `rotateVector(a, n)` which takes a vector `a` and an integer `n` and returns a new vector where each element in `a` is shifted to the right by `n` indices. For example, if `a` is `c(1, 2, 3, 4)` and `n` is `1`, the result should be `c(4, 1, 2, 3)`, but if `n` is `-1`, the result should be `c(2, 3, 4, 1)`. If `n` is larger than the length of `a`, the function should continue to rotate the vector beyond its starting point. So, if `a = c(1, 2, 3, 4)` and `n = 5`, then the result should be `a = c(4, 1, 2, 3)`.
### 7) Read and reflect [SOLO, 10%]
Read and reflect on next week's readings on [strings](r7-strings.html). 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.