#1 Assign values to variables
x <- 1.1
a <- 2.2
b <- 3.3
Compute each expr4ession and store it in z Expression 1:
z <- x^(a^b)
print(z)
## [1] 3.61714
Expression 2:
z <- (x^a)^b
print(z)
## [1] 1.997611
Expression 3:
z <- 3*x^3 + 2*x^2 + 1
print(z)
## [1] 7.413
#2 Vector 1:
vec1 <- c(seq(1,8,1), seq(7,1,-1))
print(vec1)
## [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
Vector 2:
vec2 <- rep(1:5, times = 1:5)
print(vec2)
## [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
Vector 3:
vec3 <- rep(5:1, times = 1:5)
print(vec3)
## [1] 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1
#3 Generate 2 random uniform numbers (x,y) between 0 and 1
set.seed(42) #setting for reproducibility
coords <- runif(2, min=0, max=1)
x <- coords[1]
y <- coords[2]
Convert to polar coordinates (r, theta)
r <- sqrt(x^2 + y^2) #compute radius
theta <- atan2(y,x) #compute angle in radians
Print results
cat("Caresian Coordinates: (x,y) =", x, y, "\n")
## Caresian Coordinates: (x,y) = 0.914806 0.9370754
cat("Polar Coordinates: (r, theta) =", r, theta, "\n")
## Polar Coordinates: (r, theta) = 1.309573 0.7974229
#4 Initial queue
queue <- c("sheep", "fox", "owl", "ant")
print(queue)
## [1] "sheep" "fox" "owl" "ant"
queue <- c(queue, "serpent")
print(queue)
## [1] "sheep" "fox" "owl" "ant" "serpent"
queue <- queue[-1]
print(queue)
## [1] "fox" "owl" "ant" "serpent"
queue <- c("donkey", queue)
print(queue)
## [1] "donkey" "fox" "owl" "ant" "serpent"
queue <- queue[queue != "serpent"]
print(queue)
## [1] "donkey" "fox" "owl" "ant"
queue <- queue[queue != "owl"]
print(queue)
## [1] "donkey" "fox" "ant"
queue <- append(queue, "aphid", after = match("ant", queue) -1)
print(queue)
## [1] "donkey" "fox" "aphid" "ant"
aphid_position <- match("aphid", queue)
cat("The aphid is at position:", aphid_position, "\n")
## The aphid is at position: 3
#5 Generate sequence from 1 to 100
numbers <- 1:100
apply filtering condiiton
filtered_numbers <- numbers[numbers %% 2 != 0 & numbers %% 3 != 0 & numbers %% 7 != 0]
Print each condition’s TRUE/FALSE values
print(filtered_numbers)
## [1] 1 5 11 13 17 19 23 25 29 31 37 41 43 47 53 55 59 61 65 67 71 73 79 83 85
## [26] 89 95 97