R Vector range indexing
Read the middle batch of an order queue using a range.
A simple example
Vector range indexing
- Start with A, B, C
- Do this Select the first two items
- Result A, B
Syntax
orders[start:end]
Run these examples in R
Save one example at a time as Lesson.R. Use R 4.x.
Rscript --vanilla Lesson.RSelect two orders for the packing bench
A packing bench takes a small batch from the middle of the day's queue. The supervisor wants orders B and C on this bench while the other orders stay visible in the full queue.
orders <- c("A", "B", "C", "D")
cat(paste(orders[2:3], collapse = ", "))
B, C
The requested range contains only orders B and C.
Prepare the next workshop roster
A teacher reads a workshop roster in arrival order. The coordinator prints the first two entries on a separate slip. Which entries appear, in order?
Work from the delivery round
A courier has three labelled stops in route order. The coordinator prints the first two entries on a separate slip. Which entries appear, in order?
What to watch for
R includes both positions in a colon range and starts at one. For a dynamically empty range, check its length first.