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

a) xab

b) (xa)b

c) 3x3+2x2+1

d) The digit in the second place of z (hint: use floor() and/or &&)

x <- 1.1
a <- 2.2
b <- 3.3

a.

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

b.

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

c.

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

d.

floor((z %% 1)*10) # %% divides first value by second value and gives you the remainder # floor truncates after decimal place
## [1] 4
trunc((z %% 1)*10) #trunc also truncates after decimal place
## [1] 4

2. 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)

a.

c(seq(from=1,to=8),seq(from=7,to=1))
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

b.

rep(seq(1,5), seq(1,5))
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

c.

rep(seq(from=5,to=1), seq(1,5))
##  [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).

z <- runif(2)
print(z)
## [1] 0.5070011 0.8851015
x <- z[1]
y <- z[2]
print(x)
## [1] 0.5070011
print(y)
## [1] 0.8851015
r <- sqrt(x^2 + y^2)
print(r)
## [1] 1.020027
theta <- atan(y/x)
print(theta)
## [1] 1.050604

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

a. the serpent arrives

b. the sheep enters the ark

c. the donkey arrives and talks his way to the front of the line

d. the serpent gets impatient and leaves

e. the owl gets bored and leaves

f. the aphid arrives and the ant invites him to cut in line

g. Finally, determine the position of the aphid in the line

a.

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

b.

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

c.

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

d.

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

e.

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

f.

queue <- c(queue[1:2],"aphid",queue[3])
print(queue) #alternatively, you could append function: append(queue, "apid", after = length(2))
## [1] "donkey" "fox"    "aphid"  "ant"

g.

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.

c <- seq(1,100)
d <- (c %% 2 != 0) & (c %% 3 != 0) & (c %% 7 != 0)
print(c[d]) # this is saying that d is numbers in c (the sequence of 1-100) that when divided by 2, 3, and 7, has a non-zero remainder (zero remainder means that it IS divisible by 2, 3, or 7
##  [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.

a. 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

b. Making successive copies of z, transform your vector of uniform numbers in the following ways:

  • log (base 10) of z

  • z2

  • ez

  • square root of z

    c. for each case calculate your vector of 3 numbers to get the new proportions.

z <- runif(1000)

a.

a <- sum(z < 0.10)/1000
b <- sum(z > 0.90)/1000
c <- sum(z < 0.55 & z > 0.45)/1000
d <- c(a,b,c)
print(d)
## [1] 0.096 0.101 0.092

b and c.

z <- log10(z)
head(z)
## [1] -0.073811135 -0.734016352 -0.507543943 -0.004177527 -0.282806689
## [6] -0.447377350
a <- sum(z < 0.10)/1000
b <- sum(z > 0.90)/1000
c <- sum(z < 0.55 & z > 0.45)/1000
d <- c(a,b,c)
print(d)
## [1] 1 0 0
z <- z^2
head(z)
## [1] 5.448084e-03 5.387800e-01 2.576009e-01 1.745174e-05 7.997962e-02
## [6] 2.001465e-01
a <- sum(z < 0.10)/1000
b <- sum(z > 0.90)/1000
c <- sum(z < 0.55 & z > 0.45)/1000
d <- c(a,b,c)
print(d)
## [1] 0.525 0.110 0.033
z <- exp(1)^z
a <- sum(z < 0.10)/1000
b <- sum(z > 0.90)/1000
c <- sum(z < 0.55 & z > 0.45)/1000
d <- c(a,b,c)
print(d)
## [1] 0 1 0
z <- sqrt(z)
head(z)
## [1] 1.002728 1.309166 1.137463 1.000009 1.040800 1.105252
a <- sum(z < 0.10)/1000
b <- sum(z > 0.90)/1000
c <- sum(z < 0.55 & z > 0.45)/1000
d <- c(a,b,c)
print(d)
## [1] 0 1 0