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?
ACauses an error
BReturns only the first element
CReturns the vector unchanged
DReturns all elements except the first one
✗ 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?
Ax[-c(2,4)]
Bx[c(-2,-4)]
Cx[c(2,4)]
Dx[-2,4]
✗ 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?
AReturns elements 1 and excludes 2
BGives an error
CReturns all elements
DReturns elements 1 and 2
✗ Incorrect
Mixing positive and negative indices in the same subset causes an error.
If x <- c(5, 10, 15, 20), what is x[-3]?
Ac(5, 10, 20)
Bc(5, 10, 15)
Cc(15, 20)
Dc(10, 15, 20)
✗ 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?
ATo reverse the vector
BTo select only the first element
CTo exclude unwanted elements easily
DTo sort the vector
✗ 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.
Practice
(1/5)
1. In R, what does negative indexing like x[-2] do to the vector x?
easy
A. It returns the vector x with the second element doubled.
B. It returns only the second element of x.
C. It returns the vector x without the second element.
D. It causes an error because negative indexes are not allowed.
Solution
Step 1: Understand negative indexing in R
Negative indexing excludes elements at the specified positions from the result.
Step 2: Apply negative index to x[-2]
This means the second element is removed from the vector x, returning all others.
Final Answer:
It returns the vector x without the second element. -> Option C
Quick Check:
Negative index excludes elements [OK]
Hint: Negative index removes elements by position [OK]
Common Mistakes:
Thinking negative index selects elements
Confusing negative index with subtraction
Assuming negative index causes error
2. Which of the following is the correct syntax to exclude the 1st and 3rd elements from a vector v in R?
easy
A. v[-c(1, 3)]
B. v[c(-1, 3)]
C. v[c(1, -3)]
D. v[-1, -3]
Solution
Step 1: Recall syntax for excluding multiple elements
Use negative indexing with a vector of positions inside c(), like -c(1, 3).
Step 2: Check each option
v[-c(1, 3)] correctly uses v[-c(1, 3)] to exclude 1st and 3rd elements. Others misuse negative signs or commas.
Final Answer:
v[-c(1, 3)] -> Option A
Quick Check:
Negative vector inside c() excludes multiple elements [OK]
Hint: Use negative vector inside c() to exclude multiple elements [OK]
Common Mistakes:
Placing negative signs inside c() incorrectly
Using commas instead of c() for multiple indexes
Mixing positive and negative indexes in one vector
A. Incorrect use of comma in negative indexing causing subscript error.
B. Negative indexing cannot be used with numeric vectors.
C. The vector nums is empty, so indexing fails.
D. The code runs without error and prints the vector.
Solution
Step 1: Analyze the indexing syntax
Negative indexing uses a single vector inside brackets. Using a comma like nums[-2, 3] implies matrix indexing, which is invalid for a vector.
Step 2: Identify the error type
This causes a subscript error because nums is a vector, not a matrix or data frame.
Final Answer:
Incorrect use of comma in negative indexing causing subscript error. -> Option A
Quick Check:
Comma in vector indexing causes error [OK]
Hint: No commas in vector negative indexing [OK]
Common Mistakes:
Using commas like in matrices for vectors
Assuming negative indexing works with multiple arguments
Ignoring error messages about subscripts
5. You have a vector data <- c(3, 6, 9, 12, 15, 18). You want to create a new vector excluding all elements at even positions using negative indexing. Which code correctly achieves this?
hard
A. new_data <- data[-seq(1, length(data), by=2)]
B. new_data <- data[-c(2, 4, 6, 8)]
C. new_data <- data[-c(1, 3, 5)]
D. new_data <- data[-seq(2, length(data), by=2)]
Solution
Step 1: Identify even positions in the vector
Even positions are 2, 4, 6. Using seq(2, length(data), by=2) generates these positions dynamically.
Step 2: Use negative indexing to exclude these positions
Applying negative indexing with -seq(2, length(data), by=2) removes elements at even positions.
Final Answer:
new_data <- data[-seq(2, length(data), by=2)] -> Option D
Quick Check:
Negative seq() excludes even positions [OK]
Hint: Use seq() with negative index to exclude positions [OK]
Common Mistakes:
Hardcoding indexes without checking vector length
Excluding odd positions instead of even
Using indexes outside vector length
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.