0% found this document useful (0 votes)
56 views3 pages

Miscellaneous Problems Operators

This document contains 8 exercises involving operations on vectors and matrices in R such as extracting elements, replacing values, and logical operations. The exercises demonstrate vectorization, indexing, subsetting, and other basic R programming techniques.

Uploaded by

Madhav Munupalle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views3 pages

Miscellaneous Problems Operators

This document contains 8 exercises involving operations on vectors and matrices in R such as extracting elements, replacing values, and logical operations. The exercises demonstrate vectorization, indexing, subsetting, and other basic R programming techniques.

Uploaded by

Madhav Munupalle
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

e. Compute the exponential transform of your answer to (d).

Exercise 2

a. Create an object that stores the value 32 x 41/8.


b. Overwrite your object in (a) by itself divided by 2.33.
c. Create a new object with the value -8.2 x 10-13.

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

a. Store this vector of 10 values: foo <- c(7,5,6,1,2,10,8,3,8,2).


Then, do the following:
i. Extract the elements greater than or equal to 5, storing the result as bar.
ii. Display the vector containing those elements from foo that remain after omitting all elements
that are greater than or equal to 5.
b. Use bar from (a) (i) to construct a 2 _ 3 matrix called baz, filled in a row-wise fashion. Then,
do the following:
i. Replace any elements that are equal to 8 with the squared value of the element in row 1, column
2 of baz itself.
ii. Confirm that all values in baz are now less than or equal to 25 AND greater than 4.
c. Create a 3 x 2 x 3 array called qux using the following vector of 18 values:
c(10,5,1,4,7,4,3,3,1,3,4,3,1,7,8,3,7,3). Then, do the following:
i. Identify the dimension-specific index positions of elements that are either 3 OR 4.
ii. Replace all elements in qux that are less than 3 OR greater than or equal to 7 with the value 100.
d. Return to foo from (a). Use the vector c(F,T) to extract every second value from foo.

You might also like