Recall & Review
beginner
What does negative indexing do in R when used with vectors?
Negative indexing in R excludes elements at the specified positions from a vector. For example, x[-2] returns all elements except the second one.
Click to reveal answer
beginner
How do you exclude multiple elements from a vector using negative indexing?
You provide a vector of negative indices. For example, x[-c(1,3)] excludes the first and third elements from x.
Click to reveal answer
intermediate
What happens if you mix positive and negative indices in R when subsetting a vector?
R will give an error because you cannot mix positive and negative indices in the same subset operation.
Click to reveal answer
beginner
Example: Given x <- c(10, 20, 30, 40), what is the result of x[-2]?
The result is c(10, 30, 40) because the second element (20) is excluded.
Click to reveal answer
beginner
Why is negative indexing useful in data manipulation?
It allows you to quickly remove unwanted elements without creating a new vector or using complex functions.
Click to reveal answer
What does x[-1] do in R if x is a vector?
✗ Incorrect
Negative indexing excludes the element at that position, so x[-1] excludes the first element.
Which of these is a valid way to exclude the 2nd and 4th elements from a vector x?
✗ Incorrect
Only C: x[-c(2,4)] is a valid way to exclude the 2nd and 4th elements. A selects them instead. B is invalid because mixing negative indices inside c() is not allowed. D is invalid syntax for vector indexing.
What happens if you try x[c(1, -2)] in R?
✗ Incorrect
Mixing positive and negative indices in the same subset causes an error.
If x <- c(5, 10, 15, 20), what is x[-3]?
✗ Incorrect
x[-3] excludes the third element (15), so the result is c(5, 10, 20).
Why might you use negative indexing instead of positive indexing?
✗ Incorrect
Negative indexing is a quick way to exclude elements without specifying all the ones you want.
Explain how negative indexing works in R and give an example.
Think about removing elements by position.
You got /3 concepts.
What error occurs if you mix positive and negative indices in R? Why?
Consider how R expects indexing to be consistent.
You got /3 concepts.