Question
using Rstudio # Take a vector below. Assume that we start summation from the first element adding element by element. Our task is to find
using Rstudio
# Take a vector below. Assume that we start summation from the first element adding element by element. Our task is to find the maximal number of elements we can take while the total summation stays below a given threshold.
For example: x <- c(1, 2, 3, 4, 5, 6). This summation (or running total, or cumulative sum) is c(1, 3, 6,10,15,21). Hence, for the summation to stay below a threshold 13 you can take 4 first numbers.
x <- runif(50) # our test vector
y <- 10 # our threshold
We have to make two custom functions, each function should take a vector of numbers and threshold level; and it should return the number of values to take before we hit a threshold.
# First function should use control flow operators and no vectorisation.
# Second function should use vectorisation/indexing and function cumsum(), but no control flow operators.
Please write down the code below:
------------code-----------
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started