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.

# Create variables
x <- 1.1
a <- 2.2
b <- 3.3

# Part a
z <- x^a^b
print(z) 
## [1] 3.61714
# Part b
z <- (x^a)^b
print(z)
## [1] 1.997611
# Part c
z <- 3*x^3 + 2*x^2 +1
print(z)
## [1] 7.413
# Part d
z <- floor((z %% 1)*10)
print(z)
## [1] 4

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)
# Part a
a <- rep(1:8, 1)
b <- rep(7:1, 1)
c <- c(a,b)
print(c)
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
# Part b
z <- c(1,rep(2, each = 2),rep(3, each = 3),rep(4, each = 4),rep(5, each = 5))
z <- rep(1:5, c(1:5))
print(z)
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
# Part c
z <- rep(5:1, c(1:5))
print(z)
##  [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 (If you don’t know what polar coordinates are, read about them on the web or in your calculus textbook).

xy <- runif(2)
print(xy)
## [1] 0.9574480 0.6108419
r <- sqrt((xy[1])^2 + (xy[2])^2)
theta <- atan((xy[2])/(xy[1]))

coordinates <- c(r,theta)
print(coordinates)
## [1] 1.1357088 0.5678857

4) Suppose that queue <- c(“sheep”, “fox”, “owl”, “ant”) and that 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 the queue successively as

  1. the serpent arrives;
  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")

# Part a
queue <- c(queue, "serpent")
print(queue)
## [1] "sheep"   "fox"     "owl"     "ant"     "serpent"
# Part b
queue <- queue[-1]
print(queue)
## [1] "fox"     "owl"     "ant"     "serpent"
# Part c
queue <- c("donkey", queue)
print(queue)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"
# Part d
queue <- queue[-5]
print(queue)
## [1] "donkey" "fox"    "owl"    "ant"
# Part e
queue <- queue[-3]
print(queue)
## [1] "donkey" "fox"    "ant"
# Part f
queue <- c(queue[1], queue[2], "aphid", queue[3])
print(queue)
## [1] "donkey" "fox"    "aphid"  "ant"
# Part g
grep("aphid", queue)
## [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.

z <- rep(1:100)
z <- z[z%%2 !=0 & z%%3 !=0 & z%%7 !=0]
print(z)
##  [1]  1  5 11 13 17 19 23 25 29 31 37 41 43 47 53 55 59 61 65 67 71 73 79
## [24] 83 85 89 95 97

6) Create a vector z of 1000 random uniform numbers.

  1. create a vector that contains 3 numbers: the proportion of the numbers in z that are less than 0.10, greater than 0.90, and between 0.45 and 0.55.

  2. Making successive copies of z, transform your vector of uniform numbers in the following ways:
  • log (base 10) of z
  • z^2
  • e^z
  • square root of z
  1. for each case calculate your vector of 3 numbers to get the new proportions.
z <- runif(1:1000)

# Part a 
y <- c(sum(z < 0.10)/1000, sum(z > 0.90)/1000, sum(z > 0.45 & z < 0.55)/1000)
print(y)
## [1] 0.096 0.090 0.090
#Parts b and c

# 1
z <- log10(z)
a <- c(sum(z < 0.10)/1000, sum(z > 0.90)/1000, sum(z > 0.45 & z < 0.55)/1000)
print(a)
## [1] 1 0 0
# 2
z <- z^2
b <- c(sum(z < 0.10)/1000, sum(z > 0.90)/1000, sum(z > 0.45 & z < 0.55)/1000)
print(b)
## [1] 0.513 0.111 0.037
# 3
z <- exp(z)
c <- c(sum(z < 0.10)/1000, sum(z > 0.90)/1000, sum(z > 0.45 & z < 0.55)/1000)
print(c)
## [1] 0 1 0
# 4
z <- sqrt(z)
d <- c(sum(z < 0.10)/1000, sum(z > 0.90)/1000, sum(z > 0.45 & z < 0.55)/1000)
print(d)
## [1] 0 1 0