Question
Write a function called `leaps` that takes two arguments: * `x` - a vector of numeric values * `lag` - defaults to 1 Have the
Write a function called `leaps` that takes two arguments:
* `x` - a vector of numeric values * `lag` - defaults to 1
Have the function return the vector of `x`'s "leaps", defined as the (absolute value) of the differences between elements of `x` separated by `lag` elements.
For example, imagine `x` is the vector `c(5.1, 4.9, 4.7, 4.6, 5.0, 5.4)` and `lag` is 1. The function would return a vector with elements 0.2 (absolute value of 4.9-5.1), 0.2 (absolute value of 4.7-4.9), 0.1 (absolute value of 4.6-4.7), 0.4 (absolute value of 5.0-4.6), and 0.4 (absolute value of 5.4-5.0). If `lag` is 3, the function would return a vector with elements 0.5 (absolute value of 4.6-5.1), 0.1 (absolute value of 5.0-4.9), and 0.7 (absolute value of 5.4-4.7).
Have your function be able to output four different warnings:
* if `x` is not of class "numeric" and is not of class "integer", the message should say: "x should be a numeric vector". * if `lag` is not an integer (i.e. `lag%%1` is not 0; you may assume it will always be a number), the message should say: "lag should be an integer". * if `lag` is "too big", i.e., if `lag` equals the number of elements in `x` or larger, the message should say: "value of lag should be fewer than the number of elements of x". If this is the case, the function should immediately return `NA` and not run any additional 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