Link to Home

Question 1

# Defining variables
x <- 1.1
a <- 2.2
b <- 3.3

# a
z <- x^b^a
print(z)
## [1] 3.735427
# b
z <- (x^a)^b
print(z)
## [1] 1.997611
# c
z <- 3*x^3 + 2*x^2 + 1
print(z)
## [1] 7.413

Question 2

# a
c(seq(from = 1, to = 8), seq(from = 7, to = 1))
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
# b
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
#c
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

# Cartesian coordinates
x <- runif(1)
y <- runif(1)   

print(x)
## [1] 0.9157363
print(y)
## [1] 0.8631834
# Polar coordinates
r <- sqrt(x^2 + y^2)
theta <- atan(y/x)  

print(r)
## [1] 1.258435
print(theta)
## [1] 0.7558647

Question 4

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

# a
queue <- append(queue, "serpent", after = 4)
print(queue)
## [1] "sheep"   "fox"     "owl"     "ant"     "serpent"
# b
queue <- queue[-1]
print(queue)
## [1] "fox"     "owl"     "ant"     "serpent"
# c
queue <- append(queue, "donkey", after = 0)
print(queue)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"
# d
queue <- queue[-5]
print(queue)
## [1] "donkey" "fox"    "owl"    "ant"
# e
queue <- queue[-3]
print(queue)
## [1] "donkey" "fox"    "ant"
# f
queue <- append(queue, "aphid", after = 2)
print(queue)
## [1] "donkey" "fox"    "aphid"  "ant"
# g
which(queue == "aphid")
## [1] 3

Question 5

vector5 <- seq(100)
vector5 <- vector5[-which(vector5 %% 2 != 0 | vector5 %% 3 != 0 | vector5 %% 7 != 0)]
print(vector5)
## [1] 42 84