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.
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...
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.
Using R program and with a For loop. Assuming a data set of 1000 observations and...
Using R program and with a For loop. Assuming a data set of 1000 observations and 10 predictors. How would one use a for loop to cycle through different proportions of training and test sizes. For example, 20% of data goes to training and 80% for test in first iteration. Each iteration adding another 10% to the training set. So first set= (20% train, 80% test), second set = (30% train, 70% test), third set= (40% train,60%test) and so on....
Given the vector function r(t)=〈√t , 1/(t-1) ,e^2t 〉 a) Find: ∫ r(t)dt b) Calculate the...
Given the vector function r(t)=〈√t , 1/(t-1) ,e^2t 〉 a) Find: ∫ r(t)dt b) Calculate the definite integral of r(t) for 2 ≤ t ≤ 3 can you please provide a Matlab code?
Find (without using a calculator) the absolute extreme values of the function on the given interval....
Find (without using a calculator) the absolute extreme values of the function on the given interval. f(x) = 3x2 − x3 on [0, 3]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT