Challenge - 5 Problems
Negative Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of negative indexing to exclude elements
What is the output of the following R code?
R Programming
x <- c(10, 20, 30, 40, 50) result <- x[-c(2, 4)] print(result)
Attempts:
2 left
💡 Hint
Negative indexing in R removes the elements at the specified positions.
✗ Incorrect
Using negative indices in R excludes those elements from the vector. Here, positions 2 and 4 (values 20 and 40) are removed, leaving 10, 30, and 50.
❓ Predict Output
intermediate2:00remaining
Negative indexing with single element exclusion
What will be printed by this R code?
R Programming
y <- c('a', 'b', 'c', 'd') print(y[-3])
Attempts:
2 left
💡 Hint
Negative indexing removes the element at the given position.
✗ Incorrect
The element at position 3 ('c') is excluded, so the output is 'a', 'b', and 'd'.
🔧 Debug
advanced2:00remaining
Identify the error in negative indexing usage
What error does this R code produce?
R Programming
z <- c(5, 10, 15, 20) print(z[-c(2, -3)])
Attempts:
2 left
💡 Hint
Negative and positive indices cannot be mixed in the same subscript.
✗ Incorrect
In R, you cannot mix negative and positive indices in the same vector for subsetting. Here, -3 is negative but inside c(2, -3) mixes positive 2 and negative -3, causing an error.
🧠 Conceptual
advanced1:30remaining
Length of vector after negative indexing exclusion
Given the vector v <- 1:8, what is the length of v[-c(1, 3, 5)]?
Attempts:
2 left
💡 Hint
Negative indexing removes the specified elements from the vector.
✗ Incorrect
Removing elements at positions 1, 3, and 5 from an 8-element vector leaves 5 elements.
❓ Predict Output
expert2:30remaining
Output of negative indexing with repeated indices
What is the output of this R code?
R Programming
w <- c(2, 4, 6, 8, 10) print(w[-c(2, 2, 4)])
Attempts:
2 left
💡 Hint
Repeated indices in negative indexing exclude the element only once.
✗ Incorrect
Positions 2 and 4 are excluded. Repeating 2 does not cause error or exclude more elements. Remaining elements are at positions 1, 3, and 5: 2, 6, 10.