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
<-1.1
x<-2.2
a<-3.3
b<-function(z=NULL){
square<-(x^a^b)
zprint(z)
}square(z=x)
## [1] 3.61714
b. (xa)b
<-1.1
x<-2.2
a<-3.3
b<-function(z=NULL){
square<-(x^a)^b
zprint(z)
}square(z=x)
## [1] 1.997611
c. 3x3+2x2+1
<-1.1
x<-3*(x^3)+2*(x^2)+1
zprint (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
<- c("sheep", "fox", "owl", "ant")
queue <-c(queue[1:4], "serpent")
queueprint (queue)
## [1] "sheep" "fox" "owl" "ant" "serpent"
b: the sheep enters the club (so disappears from the line)
<-c(queue[-1]) # remove the first element of the queue
queueprint(queue)
## [1] "fox" "owl" "ant" "serpent"
c: the donkey arrives and talks his way to the front of the line
<-c("donkey", queue[1:4]) # add this element to the first position of my queue
queueprint(queue)
## [1] "donkey" "fox" "owl" "ant" "serpent"
d: the serpent gets impatient and leaves
<-c(queue[-5])
queueprint(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)
<-append(queue, "aphid", after=3) # append aphid immediately after position 3
queueprint(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