← Back to R Course | Chapter 3: Vectors | Lesson 4 of 7

Vector Indexing

In this page:

  1. Vector Indexing

Vector Indexing

R vectors are indexed starting at 1, not 0, using square brackets like v[3]. You can also index with a vector of positions, like v[c(1, 3)], to pull out multiple elements at once. Negative indices, like v[-1], exclude that position instead of selecting it.

Warning: R indexing starts at 1, not 0 -- v[1] is the first element, unlike most other programming languages.

Example: Vector Indexing

markup
v <- c(10, 20, 30, 40, 50)

cat(v[1], "\n")
print(v[c(1, 3)])
print(v[-1])
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.