Question: The following code will randomly generate 10 values between 0 and 1, with all possible values having the same chance: ```{r} set.seed(1) x = runif(10,0,1)
The following code will randomly generate 10 values between 0 and 1, with all possible values having the same chance:
```{r} set.seed(1) x = runif(10,0,1) ```
And the following code will generate a different set of 10 values between 0 and 1:
```{r} set.seed(3) y = runif(10,0,1) ```
Write an R function that will keep adding elements from an input vector (call it `vec`) until the sum is greater than 5 and returns the sum after it has surpassed 5. Test your code on the vector `x` provided above.
```{r} #Delete this line of code (including the #) and input your code in this code chunk. ```
Try to expand your function that instead of returning the sum, prints a message saying "Sum cannot exceed 5" if the sum never surpasses 5. Test your code on the vector `y` provided above. This part of Question 4 is **bonus**. You may need a `break` statement in your `while()` loop for this part, which stops executing the `while()` loop if a certain condition is met. An example of a `break` statement in a loop follows:
```{r} for(i in 1:5){ if (i == 3){ break } print(i) } ```
As you see in the pdf file generated by R Markdown, this will only print the values 1 and 2.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
