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.

  1. \(x^{a^{b}}\)
  2. \((x^a)^b\)
  3. \(3x^3+2x^2+1\)
x <- 1.1
a <- 2.2
b <- 3.3

z <- x^{a^{b}}
print(z)
## [1] 3.61714
z <- (x^a)^b
print(z)
## [1] 1.997611
z <- 3*(x^3)+2*(x^2)+1
print(z)
## [1] 7.413

2.

Using the rep and seq functions, create the following vectors:

  1. (1,2,3,4,5,6,7,8,7,6,5,4,3,2,1)
  2. (1,2,2,3,3,3,4,4,4,4,5,5,5,5,5)
  3. (5,4,4,3,3,3,2,2,2,2,1,1,1,1,1)
a <- c(seq(from=1,to=8),seq(from=7,to=1))
print(a)
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
vect <- seq(from=1,to=5)
b <- rep(vect,times=vect)
print(b)
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
vect_2 <- seq(from=5,to=1)
c <- rep(vect_2,times=vect)
print(c)
##  [1] 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1

3.

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.

coord <- runif(2,min = 0,max = 10)
print(coord)
## [1] 7.946985 9.618633
r <- sqrt((coord[1])^2 + (coord[2])^2)

theta <- atan((coord[2])/(coord[1]))
polar <- c(r,theta)
print(polar)
## [1] 12.4768854  0.8802783

4.

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.
queue <- c("sheep", "fox", "owl", "ant")
print(queue)
## [1] "sheep" "fox"   "owl"   "ant"
queue <- c(queue,"serpent")
print(queue)
## [1] "sheep"   "fox"     "owl"     "ant"     "serpent"
queue <- setdiff(queue,"sheep")
print(queue)
## [1] "fox"     "owl"     "ant"     "serpent"
queue <- c("donkey",queue)
print(queue)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"
queue <- setdiff(queue,"serpent")
print(queue)
## [1] "donkey" "fox"    "owl"    "ant"
queue <- setdiff(queue,"owl")
print(queue)
## [1] "donkey" "fox"    "ant"
queue <- c(queue[1:2],"aphid",queue[3])
print(queue)
## [1] "donkey" "fox"    "aphid"  "ant"
which(queue=="aphid")
## [1] 3

5.

Use R to create a vector of all of the integers from 1 to 100 that are not divisible by 2, 3, or 7.

vect <- seq(1:100)
vect %% 2 != 0 & vect %% 3 !=0 & vect %% 7 !=0
##   [1]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
##  [13]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [25]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
##  [37]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [49] FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [61]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [73]  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
##  [85]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
##  [97]  TRUE FALSE FALSE FALSE
vect <- which(vect %% 2 != 0 & vect %% 3 !=0 & vect %% 7 !=0)
print(vect)
##  [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