1. Suppose x = 1.1, a = 2.2, and b = 3.3. Assign each expression to the value of the variable z and print the value stored in z.

    a. \(x^{a^b}\)

    b. \((x^a)^b\)

    c. \(3x^3+2x^2+1\)

    Solution:

    a. Computing \(z = x^{a^b}\)

x = 1.1; a = 2.2; b = 3.3 # Assigning values to variables x, a, & b
v1 = c(x,a,b)

z = v1[1]^v1[2]^v1[3] # Computing z
print(paste("z = ", z))
## [1] "z =  3.61714012551594"
  1. Computing \(z = (x^a)^b\):
z = (v1[1]^v1[2])^v1[3] # Computing z
print(paste("z = ", z))
## [1] "z =  1.99761087775261"
  1. Computing \(z = 3x^3+2x^2+1\):
z = (3*v1[1]^3)+(2*v1[1]^2)+1 # Computing z
print(paste("z = ", z))
## [1] "z =  7.413"
  1. Using the rep and seq functions, create the following vectors:

    a. (1,2,3,4,5,6,7,8,7,6,5,4,3,2,1)

    b. (1,2,2,3,3,3,4,4,4,4,5,5,5,5,5)

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

Solution:

vec_a = c(1:8, 7:1) # Vector a
print(vec_a)
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
dummy = 1:5 
vec_b = rep(dummy,dummy) # Vector b
print(vec_b)
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
vec_c = rep(rev(dummy),dummy) # Vector c
print(vec_c)
##  [1] 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1
  1. Create a vector of two random uniform numbers. In a spatial map, these can be interpreted as x and y coordinates that give the location of an individual (such as a marked forest tree in a plot that has been mapped). Using one of R’s inverse trigonometry functions (asin(), acos(), or atan()), convert these numbers into polar coordinates (If you don’t know what polar coordinates are, read about them on the web here, here, or in your calculus textbook).
char2seed("NO GREE FOR ANYBODY #Naija2024") # Set Seed

v5 = runif(2) # Create 2 random uniform numbers x and y coordinates
#print(v5)
r = sqrt(v5[1]^2 + v5[2]^2) # Compute the magnitude of the vector
#print(r)
theta = atan2(v5[2],v5[1]) # Compute the angle btw x and y 
#print(theta)
print(paste("r,theta = ", r,", ",theta))
## [1] "r,theta =  0.513719148137284 ,  1.08628528587438"
  1. Create a vector queue = c(“sheep”, “fox”, “owl”, “ant”) where queue represents the animals that are lined up to enter Noah’s Ark, with the sheep at the front of the line. Using R expressions, update queue as:
  1. the serpent arrives and gets in line;
  2. the sheep enters the ark;
  3. the donkey arrives and talks his way to the front of the line;
  4. the serpent gets impatient and leaves;
  5. the owl gets bored and leaves;
  6. the aphid arrives and the ant invites him to cut in line.
  7. Finally, determine the position of the aphid in the line.

Solution:

queue = c("sheep", "fox", "owl", "ant") # Creating the vector 'queue'
print(queue)
## [1] "sheep" "fox"   "owl"   "ant"
# a. the serpent arrives and gets in line;
queue = c(queue, "serpent") 
print(queue)
## [1] "sheep"   "fox"     "owl"     "ant"     "serpent"
# b. the sheep enters the ark
queue[-1] 
## [1] "fox"     "owl"     "ant"     "serpent"
# c. the donkey arrives and talks his way to the front of the line
queue[1] = "donkey" 
print(queue)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"
# d. the serpent gets impatient and leaves;
queue[-5] 
## [1] "donkey" "fox"    "owl"    "ant"
# e. the owl gets bored and leaves
queue [-3] 
## [1] "donkey"  "fox"     "ant"     "serpent"
# f. the aphid arrives and the ant invites him to cut in line.
queue[3] = "aphid" 
print(queue)
## [1] "donkey"  "fox"     "aphid"   "ant"     "serpent"
#g. Finally, determine the position of the aphid in the line.
match("aphid", queue) 
## [1] 3
  1. Use R to create a vector of all of the integers from 1 to 100 that are not divisible by 2, 3, or 7. You will need one of the arithmetic operators on this cheat sheet.

Solution:

 d = 1:100 # Create a vector of integers from 1 to 100
d1 <- d[!(d %% 2 == 0 | d %% 3 == 0 | d %% 7 == 0)] # Filter out integers that are  not divisible by 2, 3, or 7

print(d1)
##  [1]  1  5 11 13 17 19 23 25 29 31 37 41 43 47 53 55 59 61 65 67 71 73 79 83 85
## [26] 89 95 97