Question

In: Statistics and Probability

Given a vector of numeric values. with a R function using loop. testdouble(data). that returns TRUE...

Given a vector of numeric values. with a R function using loop. testdouble(data). that returns TRUE if all the even indexs elements of the vector are twice their preceding value, other wise your function returns FALSE. You can assume that the given vector has an even number of values.

TRUE scenarios:

c(3, 6, 5, 10, 11, 22, 13, 26)

c(0, 0,1, 2, 2, 4, 3, 6)

FALSE scenarios:

c(3, 7, 5, 6, 11, 22, 13, 26)

c(0, 2, 1, 2, 2, 4, 3, 6)

Now, without using any loop, write your own R function, testdouble2(data), taht returns TRUE, if all the even index elements of the vector are twice their preceding value, otherwise your function returns FALSE.

Solutions

Expert Solution

The R codes fopr two functions with loop and without loop are given below:

#data <- c(3, 6, 5, 10, 11, 22, 13, 26)
#data <-c(0, 0,1, 2, 2, 4, 3, 6)
data <-c(3, 7, 5, 6, 11, 22, 13, 26)
#data <- c(3, 6, 5, 10, 11, 22, 13, 26)
#data <-c(0, 0,1, 2, 2, 4, 3, 6)
testdouble <- function(data)
{
n <- 0:((length(data)/2)-1)
n <- 2*n+1
for(i in n)
{
if (data [i+1] != 2*data[i])
{
return (FALSE)
}
  
}
return (TRUE)

}

testdouble2 <- function(data)
{
n <- 1:(length(data)/2)
vec <- data[2*n] == 2*data[2*n-1]
if (length(which(vec == FALSE))>0)
{
return (FALSE)
}
return (TRUE)
}

testdouble2(data)
testdouble(data)

Sample output:

> 
> testdouble2(data)
[1] FALSE
> testdouble(data)
[1] FALSE

No checking is done for the argument length. Even number of array length >= 2 is assumed.

If you want checking whether the data length is even and greater tha 0, add additional code.


Related Solutions

Here is the R code for running a t-test: t.test( numeric vector of data values, another...
Here is the R code for running a t-test: t.test( numeric vector of data values, another optional numeric vector of data values,        alternative = c("two.sided", "less", "greater"),        mu = Ho, paired = c(TRUE, FALSE), var.equal = c(TRUE,FALSE),conf.level =1-) 1.) Suppose 30 students are all taking the same Math 115 and English 101 classes at CSUN. You want to know in which class students tend to do better. The data below represents the class averages of the students in both classes....
Here is the R code for running a t-test: t.test( numeric vector of data values, another...
Here is the R code for running a t-test: t.test( numeric vector of data values, another optional numeric vector of data values,        alternative = c("two.sided", "less", "greater"),        mu = Ho, paired = c(TRUE, FALSE), var.equal = c(TRUE,FALSE),conf.level =1-) 2) You want to determine if the average height of men in California is greater than the average height of men in Nebraska. You take a random sample of 30 men in California and 30 men in Nebraska. The data below represents...
Write a function in R named counts. This function should take as parameters a numeric vector...
Write a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x),max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x),b1),I2 = [b1,b − 2),...,In = (bn−1,max(x)]. Note that since the intervals are equally sized, the value of bi is constrained. The function will then return a...
In R, Create a function that replaces the negative values in a numeric matrix with a...
In R, Create a function that replaces the negative values in a numeric matrix with a random integer between 1 and 10. At the same time, it counts the negative values and prints their number. Apply the function to a 4 by 5 matrix of random continuous uniform values ranging from -10 to 10.
In R: write a function that inputs a vector x and a number n and returns...
In R: write a function that inputs a vector x and a number n and returns the first n elements of x. When n is greater than length(x), your function should just return x. We are not allowed to use any control flow statements
C++ Given vector<float> vec; Using a ranged for loop, modify each value in vector to cube...
C++ Given vector<float> vec; Using a ranged for loop, modify each value in vector to cube and subtract 12.5
Data Structures for R studio Create a numeric vector containing 10 random numbers ranging from 1...
Data Structures for R studio Create a numeric vector containing 10 random numbers ranging from 1 to 10000. Validate that the object you created is a vector Give an example of where you might find this in a biologicall data set. Create a 10-member list containg both numeric and character data. Validate that the object you created is a list Give an example of where you might find this in a biologicall data set. Create a data frame that relates...
Use a while(true) loop to ask the user the following 2 values “Enter a rate r...
Use a while(true) loop to ask the user the following 2 values “Enter a rate r =” “Enter a nonnegative integer (enter negative integer to quit):” If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the partial sum for the geometric series, namely 1 + r + rˆ2 + rˆ3 + . . . +rˆn. Use...
Write a function that takes a numeric or integer vector and adds up only the numbers...
Write a function that takes a numeric or integer vector and adds up only the numbers whose integer parts are even. Modify your answer to question above to include an option that allows you to choose whether to sum numbers whose integer parts are even or are odd. Your function should have as a default that it gives the same output as the function in question 4. In other words, if the user doesn’t specify whether to sum evens or...
Using Matlab, write a function that takes numeric data as its input argument and prints a...
Using Matlab, write a function that takes numeric data as its input argument and prints a message to the Command Window stating if the number is positive, or negative, or 0. This function should transfer an output value to the Workspace ONLY when the input value is negative. Please include a copiable code and the associated screenshots.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT