Assignment3

Abigail Griffin

2023-01-22

Daily Assignment 3

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

a. x^a^b

x<-1.1
a<-2.2
b<-3.3
square<-function(z=NULL){
  z<-(x^a^b)
  print(z)
}
square(z=x)
## [1] 3.61714

b. (xa)b

x<-1.1
a<-2.2
b<-3.3
square<-function(z=NULL){
  z<-(x^a)^b
  print(z)
}
square(z=x)
## [1] 1.997611

c. 3x3+2x2+1

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

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)

c((seq(from=1, to=8)), (seq(from=7, to=1))) # combine these values into a list
##  [1] 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)

rep(1:5, 1:5) # repeat 1-5 in increasing increments (+1) until 5
##  [1] 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)

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

3: Create a vector using the code: queue <- c("sheep", "fox", "owl", "ant"), where queue represents the animals that are lined up to enter an exclusive club, with the sheep at the front of the line. Using R expressions, update queue as:

a: the serpent arrives and gets in the back of the line

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

b: the sheep enters the club (so disappears from the line)

queue<-c(queue[-1]) # remove the first element of the queue
print(queue)
## [1] "fox"     "owl"     "ant"     "serpent"

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

queue<-c("donkey", queue[1:4]) # add this element to the first position of my queue
print(queue)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"

d: the serpent gets impatient and leaves

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

e: the aphid arrives and the ant invites him to cut in line (hint: check out the append function)

queue<-append(queue, "aphid", after=3) # append aphid immediately after position 3
print(queue)
## [1] "donkey" "fox"    "owl"    "aphid"  "ant"

f: Finally, determine the position of the aphid in the line (hint: use the which function).

which(queue=="aphid") # asks which position is the aphid in my queue?
## [1] 4