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.