Concept Flow - Vector indexing (1-based)
Start with vector
Choose index i (1-based)
Access element at position i
Return element value
End
In R, vectors start counting at 1. You pick an index starting from 1 to get the element at that position.
vec <- c(10, 20, 30, 40) index <- 2 value <- vec[index] print(value)
| Step | Variable | Value | Action | Output |
|---|---|---|---|---|
| 1 | vec | c(10, 20, 30, 40) | Create vector | |
| 2 | index | 2 | Set index to 2 | |
| 3 | value | vec[2] | Access element at position 2 | 20 |
| 4 | print(value) | Print the value | 20 | |
| 5 | End of execution |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| vec | undefined | c(10, 20, 30, 40) | c(10, 20, 30, 40) | c(10, 20, 30, 40) | c(10, 20, 30, 40) |
| index | undefined | undefined | 2 | 2 | 2 |
| value | undefined | undefined | undefined | 20 | 20 |
Vector indexing in R starts at 1. Use vec[i] to get the element at position i. Index 1 is the first element. Accessing index 0 returns empty. Always count positions starting from 1.