Recall & Review
beginner
What does 1-based indexing mean in R vectors?
It means the first element of a vector is accessed with index 1, not 0 like in some other languages.
Click to reveal answer
beginner
How do you access the third element of a vector
v in R?Use
v[3] to get the third element because R starts counting at 1.Click to reveal answer
intermediate
What happens if you try to access an index that is out of range in an R vector?
R returns
NA (missing value) instead of an error.Click to reveal answer
beginner
How can you access multiple elements at once in an R vector?
Use a vector of indices inside the square brackets, e.g.,
v[c(1,3,5)] to get elements 1, 3, and 5.Click to reveal answer
intermediate
Why is it important to remember R uses 1-based indexing when translating code from languages like Python?
Because Python uses 0-based indexing, so the same index number refers to different elements. Forgetting this can cause bugs.
Click to reveal answer
In R, what index accesses the first element of a vector?
✗ Incorrect
R uses 1-based indexing, so the first element is at index 1.
What does
v[0] return if v is a vector in R?✗ Incorrect
Index 0 returns an empty vector in R, not an element or error.
How do you get the 2nd and 4th elements of vector
v in R?✗ Incorrect
Use
v[c(2,4)] to select specific elements by index.What does R return if you access an index larger than the vector length?
✗ Incorrect
R returns
NA for out-of-range indices.Why might code using 0-based indexing cause bugs when ported to R?
✗ Incorrect
Because R starts counting at 1, using 0-based indices will access wrong elements or empty results.
Explain how vector indexing works in R and how it differs from 0-based indexing languages.
Think about how you count items in a list starting from one.
You got /4 concepts.
Describe what happens when you try to access an index outside the range of an R vector.
Consider what R does when you ask for something that does not exist.
You got /3 concepts.