Miscellaneous Problems Operators
Miscellaneous Problems Operators
Exercise 1
a. Using R, simplify
6𝑎 + 42
34.2−3.62
when value of a = 2.3
b. Which of the following squares negative 4 and adds 2 to the result? Check for (i-iv). Justify it
with code.
(i) (−4)2 + 2
(ii) −42 + 2
(iii) (−4)2+2
(iv) −4(2+2)
c. Using R, how would you calculate the square root of half of the average of the numbers 25.2,
15, 16.44, 15.3, and 18.6?
d. Find loge 0.3 and compute the exponential transform of the result
Exercise 2
Exercise 3
a. Create and store a sequence of values from 5 to -11 that progresses in steps of 0.3.
b. Overwrite the object from (a) using the same sequence with the order reversed.
c. Repeat the vector c(-1,3,-5,7,-9) twice, with each element repeated 10 times, and store the
result. Display the result sorted from largest to smallest.
d. Create and store a vector that contains, in any configuration, the following:
i. A sequence of integers from 6 to 12 (inclusive)
ii. A threefold repetition of the value 5.3
iii. The number -3
iv. A sequence of nine values starting at 102 and ending at the number that is the total length of
the vector created in (c)
e. Confirm that the length of the vector created in (d) is 20.
Exercise 4
a. Create and store a vector that contains the following, in this
order:
– A sequence of length 5 from 3 to 6 (inclusive)
– A twofold repetition of the vector c(2,-5.1,-33)
7
– The value 42 + 2
b. Extract the first and last elements of your vector from (a), storing them as a new object.
c. Store as a third object the values returned by omitting the first and last values of your vector
from (a).
d. Use only (b) and (c) to reconstruct (a).
e. Overwrite (a) with the same values sorted from smallest to largest.
f. Use the colon operator as an index vector to reverse the order of (e), and confirm this is
identical to using sort on (e) with decreasing=TRUE.
g. Create a vector from (c) that repeats the third element of (c) three times, the sixth element four
times, and the last element once.
h. Create a new vector as a copy of (e) by assigning (e) as is to a newly named object. Using this
new copy of (e), overwrite the first, the fifth to the seventh (inclusive), and the last element with
the values 99 to 95 (inclusive), respectively.
Exercise 5
a. Convert the vector c(2,0.5,1,2,0.5,1,2,0.5,1) to a vector of only 1s, using a vector of length 3.
b. The conversion from a temperature measurement in degrees Fahrenheit F to Celsius C is
performed using the following equation:
5
𝐶 = (𝐹 − 32)
9
Use vector-oriented behavior in R to convert the temperatures 45, 77, 20, 19, 101, 120, and 212
in degrees Fahrenheit to degrees Celsius.
c. Use the vector c(2,4,6) and the vector c(1,2) in conjunction with rep and * to produce the
vector c(2,4,6,4,8,12).
d. Overwrite the middle four elements of the resulting vector from (c) with the two recycled
values -0.1 and -100, in that order.
Exercise 6
a. Store the following vector of 15 values as an object in your workspace:
c(6,9,7,3,6,7,9,6,3,6,6,7,1,9,1). Identify the following elements:
i. Those equal to 6
ii. Those greater than or equal to 6
iii. Those less than 6 + 2
iv. Those not equal to 6
b. Create a new vector from the one used in (a) by deleting its first three elements. With this new
vector, fill a 2 x 2 x 3 array.
Examine the array for the following entries:
i. Those less than or equal to 6 divided by 2, plus 4
ii. Those less than or equal to 6 divided by 2, plus 4, after increasing every element in the array by
2
c. Confirm the specific locations of elements equal to 0 in the
10 x 10 identity matrix I10.
d. Check whether any of the values of the logical arrays created in (b) are TRUE. If they are, check
whether they are all TRUE.
e. By extracting the diagonal elements of the logical matrix created in (c), use any to confirm there
are no TRUE entries.
Exercise 7
a. Store the vector c(7,1,7,10,5,9,10,3,10,8) as foo. Identify the elements greater than 5 OR equal
to 2.
b. Store the vector c(8,8,4,4,5,1,5,6,6,8) as bar. Identify the elements less than or equal to 6 AND
not equal to 4.
c. Identify the elements that satisfy (a) in foo AND satisfy (b) in bar.
d. Store a third vector called baz that is equal to the element-wise sum of foo and bar. Determine
the following:
i. The elements of baz greater than or equal to 14 but not equal to 15
ii. The elements of the vector obtained via an element-wise division of baz by foo that are greater
than 4 OR less than or equal to 2
e. Confirm that using the long version in all of the preceding exercises performs only the first
comparison (that is, the results each match the first entries of the previously obtained vectors).
Exercise 8