Menu
Coddy logo textTech

Vector Slicing and Indexing

Part of the Fundamentals section of Coddy's R journey — lesson 67 of 78.

You've learned how to access single elements and iterate through vectors. Now let's explore how to extract multiple elements at once using slicing. This technique lets you grab a subset of a vector in a single operation.

To extract multiple elements, pass a vector of indices inside the square brackets:

numbers <- c(10, 20, 30, 40, 50)
print(numbers[c(1, 3, 5)])

Output:

[1] 10 30 50

You can also use the colon operator to select a range of consecutive elements:

numbers <- c(10, 20, 30, 40, 50)
print(numbers[2:4])

Output:

[1] 20 30 40

Negative indices are powerful in R. They exclude elements instead of selecting them:

numbers <- c(10, 20, 30, 40, 50)
print(numbers[-2])

Output:

[1] 10 30 40 50

You can exclude multiple elements by passing a vector of negative indices:

numbers <- c(10, 20, 30, 40, 50)
print(numbers[-c(1, 5)])

Output:

[1] 20 30 40

Remember: you cannot mix positive and negative indices in the same selection. R will throw an error if you try numbers[c(1, -2)].

challenge icon

Challenge

Easy

You will receive two lines of input:

  1. A comma-separated list of numbers (e.g., 100,200,300,400,500,600,700)
  2. Two indices separated by a comma representing a range to exclude (e.g., 2,5)

Perform the following operations:

  1. Create a numeric vector from the comma-separated numbers
  2. Print elements at positions 1, 3, and 5 using a vector of indices
  3. Print elements from position 2 to position 4 using the colon operator
  4. Print the vector with the element at the first given index excluded (using negative indexing)
  5. Print the vector with elements from the first index to the second index excluded (using negative indexing with a range)

For example, if the inputs are:

10,20,30,40,50,60,70
3,5

The output should be:

[1] 10 30 50
[1] 20 30 40
[1] 10 20 40 50 60 70
[1] 10 20 60 70

Explanation:

  • Elements at positions 1, 3, 5 are: 10, 30, 50
  • Elements from position 2 to 4 are: 20, 30, 40
  • Excluding position 3 gives: 10, 20, 40, 50, 60, 70
  • Excluding positions 3 through 5 gives: 10, 20, 60, 70

Cheat sheet

To extract multiple elements from a vector, pass a vector of indices inside square brackets:

numbers <- c(10, 20, 30, 40, 50)
print(numbers[c(1, 3, 5)])  # [1] 10 30 50

Use the colon operator to select a range of consecutive elements:

numbers <- c(10, 20, 30, 40, 50)
print(numbers[2:4])  # [1] 20 30 40

Negative indices exclude elements instead of selecting them:

numbers <- c(10, 20, 30, 40, 50)
print(numbers[-2])  # [1] 10 30 40 50

Exclude multiple elements by passing a vector of negative indices:

numbers <- c(10, 20, 30, 40, 50)
print(numbers[-c(1, 5)])  # [1] 20 30 40

Important: You cannot mix positive and negative indices in the same selection.

Try it yourself

# Read input
con <- file("stdin", "r")
numbers_input <- suppressWarnings(readLines(con, n = 1))
indices_input <- suppressWarnings(readLines(con, n = 1))
close(con)

# Parse the comma-separated numbers into a numeric vector
numbers <- as.numeric(strsplit(numbers_input, ",")[[1]])

# Parse the two indices
indices <- as.numeric(strsplit(indices_input, ",")[[1]])
idx1 <- indices[1]
idx2 <- indices[2]

# TODO: Write your code below
# 1. Print elements at positions 1, 3, and 5 using a vector of indices

# 2. Print elements from position 2 to position 4 using the colon operator

# 3. Print the vector with the element at idx1 excluded (negative indexing)

# 4. Print the vector with elements from idx1 to idx2 excluded (negative indexing with range)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals