Exercise 7 of 10 · Collections
Read Values from a Vector
What you will make
A program that stores several temperature readings in one vector and prints specific ones by position.
The one new idea: Vectors with c() and 1-based indexing
Vectors are R's basic way of holding a group of related values together — scores, prices, readings — and you'll build on this collection type for the rest of this course.
Go straight to the code ↓The concept
A vector is R's way of holding a group of related values as one object, created with c() — for example, c(68, 71, 75, 69) is a vector of four numbers. You can pull out any single value using square brackets and its position: readings[1] gets the first value, readings[2] gets the second, and so on. R counts positions starting at 1, not 0, which is a key difference from many other programming languages you might encounter later.
Indexing with a position that doesn't make sense, like 0, isn't a crash in R — it quietly returns a vector with nothing in it, which can make the mistake easy to miss unless you check the actual output carefully.
Worked example
colors <- c("red", "green", "blue", "yellow")
cat("Second color: ", colors[2], "\n", sep = "")
cat("Third color: ", colors[3], "\n", sep = "")Second color: green
Third color: blueHere, colors holds four strings, and colors[2] and colors[3] pull out the second and third ones by position, counting from 1. The first color, "red", would be colors[1]; there is no colors[0].
Your turn
The starter code stores four temperature readings and tries to print the first one, but it asks for readings[0], which doesn't correspond to any real position in the vector. Change the index so it correctly refers to the first element, keeping in mind that R vectors start at position 1.
If something goes wrong
If the "First reading:" line prints with nothing after the colon, that's the sign of indexing with 0 — R didn't error, it just returned an empty value. Double-check every index you use against the actual position you intend, counting from 1, and remember that the last valid index for a vector of length 4 is [4], not [3] or [5].
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Assuming readings[0] causes a crash, like it would in some other languages.
- In R, indexing with 0 doesn't error — it returns a zero-length vector, so the program keeps running but the value is silently missing from the output, which can be easy to overlook.
- Changing readings[4] to readings[3] when trying to fix the 'off by one' feeling.
- readings[4] correctly points to the last of the four values in this vector; the only mistake in the starter code is the first index, not the last one.
- Thinking indexing starts at 0 because that's common in many other languages.
- R is 1-based: the first element of any vector is always at position 1, the second at position 2, and so on — this is a deliberate and well-documented difference from languages like Python or JavaScript.
Want a blank editor instead? Open the R playground.