1

Question: 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.

x = 1.1
a = 2.2
b = 3.3

1a

z <- (x^a^b)

print(z)
## [1] 3.61714

1b

z <- ((x^a)^b)
print(z)
## [1] 1.997611

1c

z <- (3*x^3+2*x^2+1)
print(z)
## [1] 7.413

2

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

2a

seq <- c(seq(1,8), seq(7,1))
print (seq)
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

2b

rep <- c(rep(1,1), rep(2,2), rep(3,3), rep(4,4), rep(5,5))
print(rep)
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

2c

rep <- c(rep(5,1), rep(4,2), rep(3,3), rep(2,4), rep(1,5))
print(rep)
##  [1] 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1

3

Question: 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).

runif(2)
## [1] 0.7799266 0.1003272
x <-  0.9920278
y <-  0.5059488
r <- sqrt(x^2+y^2)
theta <- atan2(y, x)
c(r,theta)
## [1] 1.1135993 0.4716273

4

Question: 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:

the serpent arrives and gets in line; the sheep enters the ark; the donkey arrives and talks his way to the front of the line; the serpent gets impatient and leaves; the owl gets bored and leaves; the aphid arrives and the ant invites him to cut in line. Finally, determine the position of the aphid in the line.

4a

vector <- queue <- c("sheep", "fox", "owl", "ant")
queue <- c("sheep", "fox", "owl", "ant", "serpent")
print(queue)
## [1] "sheep"   "fox"     "owl"     "ant"     "serpent"

4b

queue <- queue [-c(1)]
print(queue)
## [1] "fox"     "owl"     "ant"     "serpent"

4c

queue <- c("donkey", "fox", "owl", "ant", "serpent")
print(queue)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"

4d

queue <- queue [-c(5)]
print(queue)
## [1] "donkey" "fox"    "owl"    "ant"

4e

queue <- queue [-c(3)]
print(queue)
## [1] "donkey" "fox"    "ant"

4f

queue <- c("donkey", "fox", "aphid", "ant")
print(queue)
## [1] "donkey" "fox"    "aphid"  "ant"

4g

queue <- queue [c(3)]
print(queue)
## [1] "aphid"

5

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

integers <- 1:100
print(integers)
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18
##  [19]  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36
##  [37]  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54
##  [55]  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72
##  [73]  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90
##  [91]  91  92  93  94  95  96  97  98  99 100
integers_new <- integers[!(integers %% 2==0| integers %% 3==0| integers %% 7==0)]
print(integers_new)
##  [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