Vector Indexing
In this page:
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
v <- c(10, 20, 30, 40, 50)
cat(v[1], "\n")
print(v[c(1, 3)])
print(v[-1])
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: