Description
1. Give R assignment statements that set the variable z to
(a) x
ab
(b) (x
a
)
b
(c) 3x
3 + 2x
2 + 6x + 1
(d) the digit in the second decimal place of x (hint: use floor(x) )
(e) z +1
2. Give R expressions that return the following matrices and vectors
(a) (1,2,3,4,5,6,7,8,7,6,5,4,3,2,1)
(b) (1,2,2,3,3,3,4,4,4,4,5,5,5,5,5)
(c)
0 1 1
1 0 1
1 1 0
(d)
0 2 3
0 5 0
7 0 0
3. Suppose vec is a vector of length 2. Interpreting vec as the coordinates of a point in
R2
, use R to express it in polar coordinates. You will need (at least one of) the inverse
trigonometric functions: acos(x) , asin(x) , and atan(x).
4. Use R to produce a vector containing all integers from 1 to 100 that are not divisible
by 2, 3, or 7.
5. Suppose that queue < −c(”Steve”, ”Russell”, ”Alison”, ”Liam”) and that queue represents
a supermarket queue with Steve first in line. Using R expressions update the supermarket queue
as successively:
(a) Barry arrives;
(b) Steve is served;
1
(c) Pam talks her way to the front with one item;
(d) Barry gets impatient and leaves;
(e) Alison gets impatient and leaves.
For the last case you should not assume that you know where in the queue Alison is
standing.
Finally, using the function which(x) , find the position of Russell in the queue.
Note that when assigning a text string to a variable, it needs to be in quotes.
6. Which of the following assignments will be successful? What will the vectors x, y, and
z look like at each stage?
rm(list = ls())
x <- 1
x[3] <- 3
y <- c()
y[2] <- 2
y[3] <- y[1]
y[2] <- y[4]
z[1] <- 0
2
