Go back to Index

  1. Assign the numbers 1 to 1000 to a variable ‘x’ . The reciprocal of a number is obtained by dividing 1 over the number (1/number). Define y as the reciprocal of x.
x <- c(1:1000)
y <- 1/x
  1. Calculate the inverse tangent (that is arctan) of y and assign the result to a variable w. Hint: take a look to the ?Trig help page to find a function that calculates the inverse tangent
w <- atan(y)
  1. Assign to a variable z the reciprocal of the Tangent of w.
z<- 1/tan(w)
  1. Compare z and x using a logical statement. Before running the command think about what we should expect.
z==x
z[1]==x[1]
  1. Note that not all elements are equal eventhough if they seem to be. Now compare the elements using de function identical, Then, use the function all.equal. Again, first read about these functions using help.
identical(x, z)
all.equal(x, z)
all.equal(x, z, tolerance=0)
  1. Most built-in functions do not apply vectorization by default. Try the following

mean(1:5)

mean(1,2,3,4,5)

# This only takes the first element. To replicate the previous result we need to vectorize:

mean(c(1,2,3,4,5))