Problem 1
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.
xab
(xa)^b
3x3+2x2+1
= 1
x = 2.2
a = 3.3 b
= x^a^b
z print(z)
## [1] 1
= (x^a)^b
z print(z)
## [1] 1
= 3*(x^3) + 2*(x^2) + 1
z print(z)
## [1] 6
Problem 2
**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)**
= c((seq(from=1,to=8)), (seq(from=7,to=1)))
s print(s)
## [1] 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1
= c(rep(1,1),rep(2,2),rep(3,3),rep(4,4),rep(5,5))
s print(s)
## [1] 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5
= c(rep(5,1),rep(4,2),rep(3,3),rep(2,4),rep(1,5))
s print(s)
## [1] 5 4 4 3 3 3 2 2 2 2 1 1 1 1 1
Problem 3
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.
= c(runif(2))
n print(n)
## [1] 0.7273111 0.6777043
= atan(n[2]/n[1])
y print(y)
## [1] 0.7501059
= sqrt((n[1]^2)+(n[2]^2))
x print(x)
## [1] 0.994115
x=r, y=theta
Problem 4
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.
= c("sheep", "fox", "owl", "ant")
queue print(queue)
## [1] "sheep" "fox" "owl" "ant"
= append(queue, "serpent")
queue print(queue)
## [1] "sheep" "fox" "owl" "ant" "serpent"
= queue[-1]
queue print(queue)
## [1] "fox" "owl" "ant" "serpent"
= append(queue, "donkey", after = 0)
queue print(queue)
## [1] "donkey" "fox" "owl" "ant" "serpent"
= queue[-5]
queue print(queue)
## [1] "donkey" "fox" "owl" "ant"
= queue[-3]
queue print(queue)
## [1] "donkey" "fox" "ant"
= append(queue, "aphid", after = 2)
queue print(queue)
## [1] "donkey" "fox" "aphid" "ant"
The aphid is 3rd in line!
Problem 5
Use R to create a vector of all of the integers from 1 to 100 that are not divisible by 2, 3, or 7.
= c(seq(from=1,to=100,by=1))
v
= which(v %% 3 != 0 & v %% 7 != 0 & v %% 2 != 0)
v2
print(v2)
## [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