Problem 1
x <- 1.1
a <- 2.2
b <- 3.3
# a
z <- x^(a^b)
print(z)
# b
z <- (x^a)^b
print(z)
# c
z <- 3*(x^3)+2*(x^2)+1
print(z)
Problem 2
# a: (1,2,3,4,5,6,7,8,7,6,5,4,3,2,1)
a <- rep(NA,15)
print(a)
a <- c(seq(1:8),seq(from=7,to=1))
print(a)
# b: (1,2,2,3,3,3,4,4,4,4,5,5,5,5,5)
b <- rep(NA,15) #initialize
b <- rep(1:5,1:5) # repeat the numbers from 1 to 5 for 1 to 5 times respectively
print(b)
# c: (5,4,4,3,3,3,2,2,2,2,1,1,1,1,1)
c <- rep(NA,15)
c <- rep(5:1,1:5) #same as above but count down 5:1
print(c)
Problem 3
xy = runif(2)
print(xy)
r = sqrt(xy[1]^2 + xy[2]^2) # pythagorean theorum
print(r)
theta = asin(xy[2] / r)# theta = arcsin(y/r)
print(theta)
rtheta = c(r,theta)
print(rtheta)
Problem 4
queue <- c("sheep", "fox", "owl", "ant")
print(queue)
# a: serpent gets in line
#queue <- c(queue,"serpent") #this works too
queue <- append(queue,"serpent")
print(queue)
# b: sheep enters the ark
queue <- queue[-1] #keep everything except index 1
print(queue)
# c: donkey to the front of the line;
queue <- c("donkey",queue)
print(queue)
# d: the serpent leaves
queue%in% "serpent"
queue <- queue[! queue%in% "serpent"] #keep all values that aren't "serpent"
print(queue)
# e: owl leaves
queue <- queue[! queue%in% "owl"] #keep all values that aren't "owl"
print(queue)
# f: aphid cuts in line ahead of the ant
# this should work - not sure why prepend function not recognized. Need a package maybe?
#queue <- prepend(queue,"aphid",before=which(queue == "ant"))
#fine, we'll append after the element before "ant". awkward but works
queue <- append(queue,"aphid",after=which(queue == "ant")-1)
print(queue)
# g: position of the aphid
aphid_position <- which(queue == "aphid")
print(aphid_position)
Problem 5
my_vector <- seq(1:100)
head(my_vector)
tail(my_vector)
div_by_2 <- my_vector %% 2 #remainder 0 when divisible by 2
print(div_by_2)
my_vector <- my_vector[! div_by_2 == 0] #keep those not divisible by 2
head(my_vector)
tail(my_vector)
my_vector <- my_vector[! my_vector %% 3 == 0] #keep those not divisible by 3
print(my_vector)
my_vector <- my_vector[! my_vector %% 7 == 0] #keep those not divisible by 7
print(my_vector)