Question

Write an R function named fifth with a single vector argument x that calculates and returns...

Write an R function named fifth with a single vector argument x that calculates and returns the sample mean of he 5th, 10th, 15th, ... elements of x. You may assume that x has at least 5 elements.

Homework Answers

Answer #1

The following code gives you the required functionality:

myFunction <- function(x)
{
   i <- 1
   j <- 1
   y <- vector()
  
   while(i<=length(x))
   {
       if( i %% 5 == 0)
       {
           y[j] <- x[i]
           j = j + 1
       }
       i = i + 1
   }
  res <- mean(y)
   return (res)
}

x <- c(1,2,3,4,5,6,7,8,9,3,5,45,6,43,3,3,43,23,23,43,23,23,32)
ans <- myFunction(x)
print(ans)

Output:

[1] 13.5

Code's link:

http://ideone.com/qZeVyX

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Use R Studio (show the code and output) Please write an R function named “outlier_detect” to...
Use R Studio (show the code and output) Please write an R function named “outlier_detect” to return or print out the outliers from a given data vector. outlier_detect = function(x){ #x: a column of numeric vector Body of code }
Write a MATLAB function function [ AvgPos, AvgNeg ] = PosNegAverage ( X ) that calculates...
Write a MATLAB function function [ AvgPos, AvgNeg ] = PosNegAverage ( X ) that calculates average of positive, AvgPos, and negative, AvgNeg, elements of array X, using the for‐end loop and if‐elseif‐else‐end selection structure. Do not use build‐in MATLAN functions in calculations. Apply the developed function for the following vector X = [ ‐7, 1, 0, 0, 12, 6, 33.2, ‐7.5 ];
IN MATLAB: Write a function file that takes a vector as an input and returns another...
IN MATLAB: Write a function file that takes a vector as an input and returns another vector with all repeated elements of the original vector. For example, the vector [3 4 1 0 4 -5 7 3] would return the vector [3 4]. Do not use the built-in function "repelem."
1. Write an R function named “abs.shift” to calculate the function value |x|-1 of a real...
1. Write an R function named “abs.shift” to calculate the function value |x|-1 of a real number x, where |x| is the absolute value of x. Do not use the abs() function. 2. Which of the following method is for simulating discrete random variables? A) The Rejection method with uniform envelope. B) The Inverse CDF method. C) The rnorm() function. D) The sample() function in R.
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except...
*** Write a function called reverse_diag that creates a square matrix whose elements are 0 except for 1s on the reverse diagonal from top right to bottom left. The reverse diagonal of an n-by-n matrix consists of the elements at the following indexes: (1, n), (2, n-1), (3, n-2), … (n, 1). The function takes one positive integer input argument named n, which is the size of the matrix, and returns the matrix itself as an output argument. Note that...
Write a Python function evaluateFunction() that takes one floating point number x as its argument and...
Write a Python function evaluateFunction() that takes one floating point number x as its argument and returns 3√(log⁡|x| )+3(x^3) The math module has a square root function and a logarithm function
I imported a dataset named "multiple.txt" into R using the read.table function. The table contains 50...
I imported a dataset named "multiple.txt" into R using the read.table function. The table contains 50 variables and 100 observations per variable. Each variable is named V1, V2, V3,...,V50. The goal is to implement multiple testing across all 50 variables using the t-stat equation T = (x-bar - 0)/(Sx/sqrt(n). I was wondering if you could post sample code to run the t-statistic equation comparing each variable sample mean to 0, making a vector with all 50 of the t-stats, and...
Write a printAll function that takes in a vector containing Cars and Trucks, and calls the...
Write a printAll function that takes in a vector containing Cars and Trucks, and calls the print() function on every Car and Truck in the vector. printAll() should call Car::print() on every Car and Truck::print() on every Truck in the vector. You may assume that print() has been declared as a virtual function. C++ programming plz help
Write a function called odd_rms that returns orms, which is the square root of the mean...
Write a function called odd_rms that returns orms, which is the square root of the mean of the squares of the first nn positive odd integers, where nn is a positive integer and is the only input argument. For example, if nn is 3, your function needs to compute and return the square root of the average of the numbers 1, 9, and 25. You may use built-in functions including, for example, sum and sqrt, except for the built-in function...