Bird
Raised Fist0
R Programmingprogramming~20 mins

Negative indexing for exclusion in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Negative Indexing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A[1] 10 20 30 40 50
B[1] 20 40
C[1] 10 30 40 50
D[1] 10 30 50
Attempts:
2 left
💡 Hint
Negative indexing in R removes the elements at the specified positions.
❓ Predict Output
intermediate
2: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])
A[1] "c"
B[1] "a" "b" "d"
C[1] "a" "b" "c" "d"
D[1] "a" "c" "d"
Attempts:
2 left
💡 Hint
Negative indexing removes the element at the given position.
🔧 Debug
advanced
2: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)])
AError: only 0's may be mixed with negative subscripts
B[1] 5 15 20
C[1] 5 10 20
DError: invalid subscript type 'list'
Attempts:
2 left
💡 Hint
Negative and positive indices cannot be mixed in the same subscript.
🧠 Conceptual
advanced
1:30remaining
Length of vector after negative indexing exclusion
Given the vector v <- 1:8, what is the length of v[-c(1, 3, 5)]?
A5
B6
C8
D3
Attempts:
2 left
💡 Hint
Negative indexing removes the specified elements from the vector.
❓ Predict Output
expert
2: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)])
A[1] 2 6 8 10
B[1] 4 8
C[1] 2 6 10
DError: invalid subscript type 'double'
Attempts:
2 left
💡 Hint
Repeated indices in negative indexing exclude the element only once.

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

  1. Step 1: Understand negative indexing in R

    Negative indexing excludes elements at the specified positions from the result.
  2. Step 2: Apply negative index to x[-2]

    This means the second element is removed from the vector x, returning all others.
  3. Final Answer:

    It returns the vector x without the second element. -> Option C
  4. 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

  1. Step 1: Recall syntax for excluding multiple elements

    Use negative indexing with a vector of positions inside c(), like -c(1, 3).
  2. 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.
  3. Final Answer:

    v[-c(1, 3)] -> Option A
  4. 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
3. What is the output of the following R code?
vec <- c(10, 20, 30, 40, 50)
result <- vec[-c(2, 4)]
print(result)
medium
A. [1] 20 40
B. [1] 10 30 50
C. [1] 10 20 30 40 50
D. [1] 30 50

Solution

  1. Step 1: Identify elements to exclude

    Negative indexing -c(2, 4) removes elements at positions 2 and 4, which are 20 and 40.
  2. Step 2: List remaining elements

    Remaining elements are at positions 1, 3, and 5: 10, 30, and 50.
  3. Final Answer:

    [1] 10 30 50 -> Option B
  4. Quick Check:

    Exclude 2nd and 4th elements [OK]
Hint: Remove elements at given positions with negative c() [OK]
Common Mistakes:
  • Including excluded elements by mistake
  • Confusing element values with positions
  • Expecting only excluded elements as output
4. Consider this R code snippet:
nums <- c(5, 10, 15, 20)
new_nums <- nums[-2, 3]
print(new_nums)

What is the error in this code?
medium
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

  1. 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.
  2. Step 2: Identify the error type

    This causes a subscript error because nums is a vector, not a matrix or data frame.
  3. Final Answer:

    Incorrect use of comma in negative indexing causing subscript error. -> Option A
  4. 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

  1. 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.
  2. Step 2: Use negative indexing to exclude these positions

    Applying negative indexing with -seq(2, length(data), by=2) removes elements at even positions.
  3. Final Answer:

    new_data <- data[-seq(2, length(data), by=2)] -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard