Question 1

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

Question 2 Using the rep and seq functions create the following vectors

pt a (1,2,3,4,5,6,7,8,7,6,5,4,3,2,1)

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

pt b (1,2,2,3,3,3,4,4,4,4,5,5,5,5,5)

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

pt c (5,4,4,3,3,3,2,2,2,2,1,1,1,1,1)

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

Question 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<-c(runif(2))
xy
## [1] 0.07763849 0.60787961
theta<-atan(xy[2]/xy[1])
theta
## [1] 1.443764
r<- sqrt((xy[2])^2+(xy[1])^2)
r
## [1] 0.6128176
polarcoord<- c(r,theta)
polarcoord
## [1] 0.6128176 1.4437639

Question 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

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

pt a the serpent arrives;

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

pt b the sheep enters the ark;

queue<- queue[2:5]
queue
## [1] "fox"     "owl"     "ant"     "serpent"

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

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

pt d the serpent gets impatient and leaves;

queue<-queue[1:4]
queue
## [1] "donkey" "fox"    "owl"    "ant"

pt e the owl gets bored and leaves;

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

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

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

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

which(queue=="aphid")
## [1] 3

Question 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

vec<- seq(1,100)
vec
##   [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
vec2<- (vec %% 2!=0)&(vec %% 3!=0)&(vec %% 7!=0)

final<-vec[vec2]
final
##  [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

Question 6

Create a vector z of 1000 random uniform numbers

z<- runif(1000)
head(z)
## [1] 0.2078232 0.2037286 0.7100279 0.4135584 0.4972874 0.5622725

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

a<-which(z<0.1)

a<-length(a)
a
## [1] 98
element1<-a/1000
element1
## [1] 0.098
b<-which(z<0.9)

b<-length(b)
b
## [1] 900
element2<-b/1000
element2
## [1] 0.9
c<-which((z>.45)&(z<.55))
c<-length(c)
c
## [1] 96
element3<-c/1000
element3
## [1] 0.096
propvec<-c(element1,element2,element3)

propvec
## [1] 0.098 0.900 0.096

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

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

log (base 10) of z

z<-log(z)
head(z)
## [1] -1.5710677 -1.5909666 -0.3424510 -0.8829566 -0.6985872 -0.5757687
a<-which(z<0.1)

a<-length(a)
a
## [1] 1000
element1<-a/1000
element1
## [1] 1
b<-which(z<0.9)

b<-length(b)
b
## [1] 1000
element2<-b/1000
element2
## [1] 1
c<-which((z>.45)&(z<.55))
c<-length(c)
c
## [1] 0
element3<-c/1000
element3
## [1] 0
logzpropvec<-c(element1,element2,element3)

logzpropvec
## [1] 1 1 0

z^2

z<- z^2
head(z)
## [1] 2.4682536 2.5311746 0.1172727 0.7796124 0.4880240 0.3315096
a<-which(z<0.1)

a<-length(a)
a
## [1] 273
element1<-a/1000
element1
## [1] 0.273
b<-which(z<0.9)

b<-length(b)
b
## [1] 612
element2<-b/1000
element2
## [1] 0.612
c<-which((z>.45)&(z<.55))
c<-length(c)
c
## [1] 39
element3<-c/1000
element3
## [1] 0.039
zsquarepropvec<-c(element1,element2,element3)

zsquarepropvec
## [1] 0.273 0.612 0.039

e^z

z<-exp(1)^z
head(z)
## [1] 11.801818 12.568261  1.124426  2.180627  1.629094  1.393069
a<-which(z<0.1)

a<-length(a)
a
## [1] 0
element1<-a/1000
element1
## [1] 0
b<-which(z<0.9)

b<-length(b)
b
## [1] 0
element2<-b/1000
element2
## [1] 0
c<-which((z>.45)&(z<.55))
c<-length(c)
c
## [1] 0
element3<-c/1000
element3
## [1] 0
ezpropvec<-c(element1,element2,element3)

ezpropvec
## [1] 0 0 0

square root of z

z<-sqrt(z)
head(z)
## [1] 3.435377 3.545174 1.060390 1.476695 1.276360 1.180284
a<-which(z<0.1)

a<-length(a)
a
## [1] 0
element1<-a/1000
element1
## [1] 0
b<-which(z<0.9)

b<-length(b)
b
## [1] 0
element2<-b/1000
element2
## [1] 0
c<-which((z>.45)&(z<.55))
c<-length(c)
c
## [1] 0
element3<-c/1000
element3
## [1] 0
sqrtpropvec<-c(element1,element2,element3)

sqrtpropvec
## [1] 0 0 0