Professional Documents
Culture Documents
Exercises 2
Exercises 2
Exercise 2
2. Negate the vector x, and construct a vector that contains the first
three values, and the last value, of x
!x
x[c(1:3, 200)]
1:3 + c(T,F,T)
intersect(1:10, 5:15)
6. Compute which elements of the set {1, 2, . . . , 10} are contained within
the set {1, 3, 5, ..., 15}.
(Try running the third command without the ()s. Why does this fail?)
7. Set all the even numbers in the vector z = [1, 2, . . . , 30] to be equal to
half their value
z <- 1:30
(z <- ifelse(z %% 2 == 0, z / 2, z))
1
Problems
1. Compute the truth table for logical OR. The R function xor computes
the logical EXCLUSIVE-OR. What is the difference between the two?
The syntax for xor is as follows: if x, y are vectors, then xor(a, b)
performs the exclusive-or operator on a,b.
3. Assign a vector x <- 1:100. Assign a vector y which contains all the
elements of x that are divisible by 3 or 5 but not both.