Question

In: Computer Science

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

Solutions

Expert Solution

Please find below R code and Screenshot and dont forget to give a Like.

Code without comments:

vec<-c(5,6,7,8,9,10,11)
func<-function(vec){
n<-5
if(length(vec)<n){
result<-vec
}
else{
result<-vec[0:n]
}
return(result)
}
print(func(vec))

Code with comments:

#creating a vector with elements
vec<-c(5,6,7,8,9,10,11)
#creating a function called func that take a vector as parameter
func<-function(vec){
n<-5
#if the length of vector is less than n it will display all the element in r
if(length(vec)<n){
#assigning all the values to the result
result<-vec
}
else{
#vector slicing for first n elements
result<-vec[0:n]
}
#returning the result
return(result)
}
#printing the result
print(func(vec))

Output 1: For given N value, the function displays the first n elements from the vector

Output 2: if size of vector is smaller than N then it displays all the elements in the vector


Related Solutions

Write a MATLAB function that accepts input vector x and returns the number of local maximums...
Write a MATLAB function that accepts input vector x and returns the number of local maximums of x with value between xmin and xmax. Ask user to input values xmin and xmax at the beginning of the procedure. Use vector x (the vector x from that file consists of 1000 numbers ranging from 0.0044 to 0.67).
Write a function that inputs the, n, the number of random trials for a standard normal...
Write a function that inputs the, n, the number of random trials for a standard normal distribution and then generates an array of n random observations (ie generate a random sample of n observations from a N(0,1) distribution. Then have the function perform a simple t-test on the sample, testing the null hypothesis that is mean 0. Have the function return two things: the results of the ttest and another variable that is set to the string= “reject” if the...
1) a) Write a MATLAB function called Area1 having two inputs, r and N, and an...
1) a) Write a MATLAB function called Area1 having two inputs, r and N, and an output, A1. The output A1 should be the area under a curve, f(x), for x starting at x_start and ending at x_end. The input r should be a vector (array) having x_start and x_end as its two elements. The input N should be an integer equal to the number of equallength sub-intervals in which the interval from x_start to x_end should be divided. Here,...
can a vector with dimensions R^N and a vector with dimensions R^N+1 be a matrix? and...
can a vector with dimensions R^N and a vector with dimensions R^N+1 be a matrix? and if so what would it dimension size be?
Write a function myfn6 which takes as inputs vector u and value a, and output as...
Write a function myfn6 which takes as inputs vector u and value a, and output as vector w with its elements being “True, ” or “False, ”(w = [True, False, False, …, True]). Such that “True, ” means a is in u and “False, ” means a is not in u. Test your code for u = [0, -3, 1, 1, 2, 2, 6, 2] and a = 9, a = 1 and a = 2. Copy your code together...
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...
Linear Algebra we know that x ∈ R^n is a nonzero vector and C is a...
Linear Algebra we know that x ∈ R^n is a nonzero vector and C is a real number. find all values of C such that ( In − Cxx^T ) is nonsingular and find its inverse knowing that its inverse is of the same form
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
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,...
Find the half-range cosine series for the function f(x) = x(n - r) in o<r<n. Deduce...
Find the half-range cosine series for the function f(x) = x(n - r) in o<r<n. Deduce that 1/14 +1/24 +1/34+…. =π4 /90. 
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT