We appreciate your visit to Write a function called checkers that takes a numeric vector x as input and outputs a new vector in a checkered pattern The new checkered. This page offers clear insights and highlights the essential aspects of the topic. Our goal is to provide a helpful and engaging learning experience. Explore the content and find the answers you need!
Answer :
#output the new vectorreturn(z) }Calling Function:checkers(1:3) Output :## [1] 1 0 2 0 3checkers(c(1:3, NA, 6:4)) Output :## [1] 1 0 2 0 3 0 6 0 5 0 4checkers(c(TRUE, FALSE, NA)) Output :## Error in checkers(c(TRUE, FALSE, NA)): x is not a numeric vector
The solution for the given problem statement: Function Definition :
checkers <- function(x) {
# Condition to check whether the input is numeric or not
if (!is.numeric(x)) {
# If the condition is true, then the following error message will be displayed
stop("x is not a numeric vector")
}
# Store the non-NA elements of the vector in a new vector named "y"
y <- x[!is.na(x)]
# Define an empty vector named "z" to store the output values
z <- numeric(length(y)*2-1)
# Loop through the indices of "y" to fill the new vector "z"
for (i in seq_along(y)) {
z[(i*2)-1] <- y[i]
if (i != length(y)) {
z[i*2] <- 0
}
}
# Return the resulting vector "z"
return(z)
}
To know more about vector visit:
https://brainly.com/question/24256726
#SPJ11
Thanks for taking the time to read Write a function called checkers that takes a numeric vector x as input and outputs a new vector in a checkered pattern The new checkered. We hope the insights shared have been valuable and enhanced your understanding of the topic. Don�t hesitate to browse our website for more informative and engaging content!
- Why do Businesses Exist Why does Starbucks Exist What Service does Starbucks Provide Really what is their product.
- The pattern of numbers below is an arithmetic sequence tex 14 24 34 44 54 ldots tex Which statement describes the recursive function used to..
- Morgan felt the need to streamline Edison Electric What changes did Morgan make.
Rewritten by : Barada