College

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 vector should start with the first element of the input vector and have zeros after each element, except the last.

- If `x` is not a numeric vector, then throw an error: "x is not a numeric vector."
- If there are `NA` values in the vector, the output should ignore them.

For example:

```R
checkers(1:3)
## [1] 1 0 2 0 3

checkers(c(1:3, NA, 6:4))
## [1] 1 0 2 0 3 0 6 0 5 0 4

checkers(c(TRUE, FALSE, NA))
## Error in checkers(c(TRUE, FALSE, NA)): x is not a numeric vector
```

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