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

# Assigning values to the variables
x = 1.1
a = 2.2
b = 3.3
# Assigning each expression to the value of the variable z
z= x^a^b
z
## [1] 3.61714
z= (x^a)^b
z
## [1] 1.997611
z= (3*x^3)+(2*x^2)+1
z
## [1] 7.413

Using the rep and seq functions, create the following vectors:

(1,2,3,4,5,6,7,8,7,6,5,4,3,2,1) (1,2,2,3,3,3,4,4,4,4,5,5,5,5,5) (5,4,4,3,3,3,2,2,2,2,1,1,1,1,1)

my_vec1 <- c(seq(1:8), seq(from=7, to=1))
print(my_vec1)
##  [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
my_vec2<- seq(1:5)
my_vec2 <- rep(x=my_vec2,times=my_vec2) 
my_vec2
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
my_vec3 <- seq(5:1)
my_vec3 <- rep(x=my_vec3,times=my_vec3) 
my_vec3
##  [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

Create a vector of two random uniform numbers. In a spatial map, these can be interpreted as x and y coordinates that give the location of an individual (such as a marked forest tree in a plot that has been mapped). Using one of R’s inverse trigonometry functions (asin(), acos(), or atan()), convert these numbers into polar coordinates (If you don’t know what polar coordinates are, read about them on the web here, here, or in your calculus textbook).

# Generate two random uniform numbers between 0 and 1
ran_num <- runif(2)

# x and y coordinates
x <- ran_num[1]
x
## [1] 0.5517967
y <- ran_num[2]
y
## [1] 0.917015
# Convert x and y coordinates to polar coordinates
r <- sqrt(x^2 + y^2) 
r
## [1] 1.070232
theta <- atan2(y, x)
theta
## [1] 1.029105
# Print the original coordinates and polar coordinates
print(paste("x:", x))
## [1] "x: 0.551796738756821"
print(paste("y:", y))
## [1] "y: 0.917014967417344"
print("Polar Coordinates:")
## [1] "Polar Coordinates:"
print(paste("Radius (r):", r))
## [1] "Radius (r): 1.07023179329064"
print(paste("Angle (theta) in radians:", theta))
## [1] "Angle (theta) in radians: 1.0291046797882"

Create a vector queue <- c(“sheep”, “fox”, “owl”, “ant”) where queue represents the animals that are lined up to enter Noah’s Ark, with the sheep at the front of the line. Using R expressions, update queue as:

the serpent arrives and gets in line; the sheep enters the ark; the donkey arrives and talks his way to the front of the line; the serpent gets impatient and leaves; the owl gets bored and leaves; the aphid arrives and the ant invites him to cut in line. Finally, determine the position of the aphid in the line.

# Create the original queue 
queue1 <- c("sheep", "fox", "owl", "ant")
print(queue1)
## [1] "sheep" "fox"   "owl"   "ant"
# the serpent arrives and gets in line
queue2 <- c(queue1, "serpent")
print(queue2)
## [1] "sheep"   "fox"     "owl"     "ant"     "serpent"
# the sheep enters the ark
queue3 <- queue2[-1]
print(queue3)
## [1] "fox"     "owl"     "ant"     "serpent"
# the donkey arrives and talks his way to the front of the line
queue4 <- c("donkey", queue3)
print(queue4)
## [1] "donkey"  "fox"     "owl"     "ant"     "serpent"
# the serpent gets impatient and leaves
queue5 <- queue4[queue4 != "serpent"]
print(queue5)
## [1] "donkey" "fox"    "owl"    "ant"
# the owl gets bored and leaves
queue6 <- queue5[queue5 != "owl"]
print(queue6)
## [1] "donkey" "fox"    "ant"
# the aphid arrives and the ant invites him to cut in line
position_ant <- which(queue6 == "ant")
queue7 <- c(queue6[1:position_ant], "aphid", queue6[(position_ant + 1):length(queue6)])
print(queue7)
## [1] "donkey" "fox"    "ant"    "aphid"  NA       "ant"
# Determine the position of the aphid in the line
position_of_aphid <- which(queue7 == "aphid")
print(queue7)
## [1] "donkey" "fox"    "ant"    "aphid"  NA       "ant"
print(paste("Position of aphid in line:", position_of_aphid))
## [1] "Position of aphid in line: 4"

Use R to create a vector of all of the integers from 1 to 100 that are not divisible by 2, 3, or 7. You will need one of the arithmetic operators on this cheat sheet.

z<- seq(100)
filtered_z <- z[!(z %% 2 == 0 | z %% 3 == 0 | z %% 7 == 0)]
print(filtered_z)
##  [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