library(ggplot2)

Homework 4 Answers

  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.)
x <- 1.1 
a <- 2.2 
b <- 3.3 

z <- x^((a)^(b))

print(z)
## [1] 3.61714
# z = 3.61714

b.)

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

c.)

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

d.)

z1 <- z %% floor(z)
print(z1)
## [1] 0.413
z2 <- floor(z1*10)


# floor ((z%% floor(z))*10 


print(z2)
## [1] 4

Answer is 4

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

a.)

vector <- (c(seq(1:8), seq(from=7, to=1)))
print(vector)
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

b.)

myVec <- seq(1:5)
rep(myVec, times= myVec)
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

c.)

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

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

set.seed(100)
b <- runif(2)
print(b)
## [1] 0.3077661 0.2576725
# Pythagorian Theorem 
r <- sqrt(b[1]^2+b[2]^2)
#Finding Theta 
theta <-  atan(b[2]/b[1]) 
print(theta)
## [1] 0.6970364
polarCoordinates <- c(r, theta) 
print(polarCoordinates)
## [1] 0.4013915 0.6970364
# (0.4013915, 0.6970364)

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

queue <- c("sheep", "fox", "owl", "ant")

#a.) the serpent arrives

queue[5] <- ("serpent")

#the sheep enters the ark;

queue <- queue[-1]
print(queue)
## [1] "fox"     "owl"     "ant"     "serpent"
# The donkey moves to the front
queue <- c("donkey", queue)
print(queue)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"
# The serpent leaves

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

queue <- queue[-3]

print(queue)
## [1] "donkey" "fox"    "ant"
# The aphid arrives and move to position in front of ant
queue <- c(queue[1], queue[2], "aphid", queue[3])
print(queue)
## [1] "donkey" "fox"    "aphid"  "ant"
# Determine position of aphid 

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.

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

z <- runif(1000)
Z2Prop <- c(mean(z<0.10),mean(z>0.90),mean(z>0.45 & z<0.55))
print(Z2Prop)
## [1] 0.091 0.113 0.093

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

z2 <- log10(z)
z2Prop <- c(mean(z2<0.10),mean(z2>0.90),mean(z2>0.45 & z2<0.55)) 
print(z2Prop)
## [1] 1 0 0
# Z squared 

z3 <- (z^2)
Z3Prop <- c(mean(z3<0.10),mean(z3>0.90), mean(z3>0.45 & z3<0.55)) 
print(Z3Prop)
## [1] 0.285 0.063 0.076
# Z^e
z4 <- exp(z) 
Z4Prop <- c(mean(z4<0.10),mean(z4>0.90), mean(z4>0.45 & z4<0.55))
print(Z4Prop)
## [1] 0 1 0
#Square root 

z5 <- sqrt(z) 
Z5Prop <- c(mean(z5<0.10),mean(z5>0.90), mean(z5>0.45 & z5<0.55))
print(Z5Prop)
## [1] 0.013 0.207 0.098