Vectors

A vector is R's most fundamental data structure — an ordered collection of values of the same type, like a single column of numbers, that R can operate on all at once.

Learn Vectors in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll create vectors with c() and sequences, pick elements out by position and by condition, and use vectorized math to transform whole vectors in a single line.

What You'll Learn in This Lesson

1️⃣ Creating Vectors

The workhorse is c() , which combines values into a vector. For regular patterns, 1:5 makes a sequence, seq() gives you control over the step, and rep() repeats values.

2️⃣ Indexing: Picking Elements

Use square brackets to pull out elements. R indexes from 1 . You can ask for a single position, several positions, a range, everything except some positions (with a minus), or — most powerfully — every element that passes a logical test.

That last line — scores[scores 90] — is the pattern you'll use constantly: filter a vector down to the values that meet a condition.

3️⃣ Vectorized Math

Operations apply to every element automatically — no loop required. You can combine a vector with a single number (R repeats the number) or with another vector (element by element). Summary functions like sum() and mean() collapse a vector to one value.

Your turn. Fill in the # TODO blank, run it, and compare with the expected output.

Write it from the outline, run it, and check it against the example output. The sum(steps 8000) trick is worth remembering — comparing a vector gives TRUE/FALSE, and summing them counts the TRUEs.

📋 Quick Reference — Vectors

Practice quiz

Which function combines values into a vector in R?

  • vector()
  • c()
  • list()
  • combine.all()

Answer: c(). c() ('combine') builds a vector from individual values.

What does 1:5 produce?

  • c(1, 2, 3, 4, 5)
  • c(1, 5)
  • c(0, 1, 2, 3, 4)
  • An error

Answer: c(1, 2, 3, 4, 5). The colon makes the integer sequence 1, 2, 3, 4, 5.

What does seq(0, 10, by = 2) return?

  • c(0, 10)
  • c(2, 4, 6, 8, 10)
  • c(0, 5, 10)
  • c(0, 2, 4, 6, 8, 10)

Answer: c(0, 2, 4, 6, 8, 10). seq() steps from 0 to 10 in increments of 2.

At what index does R start counting?

  • At 0
  • At -1
  • At 1
  • At the last element

Answer: At 1. R is 1-indexed: x[1] is the first element.

What does scores[scores >= 90] do?

  • Counts elements >= 90
  • Keeps only the elements that are 90 or greater
  • Returns positions
  • Returns TRUE/FALSE

Answer: Keeps only the elements that are 90 or greater. Logical indexing keeps elements where the test is TRUE.

What does length(ages) return?

  • The number of elements in the vector
  • The sum of the elements
  • The largest element
  • The vector itself

Answer: The number of elements in the vector. length() counts how many elements a vector has.

What does c(1, "a") become?

  • A list
  • An error
  • Numeric
  • A character vector (everything coerced to character)

Answer: A character vector (everything coerced to character). A vector holds one type; R coerces to the most flexible type, here character.

What does c(1, 2, 3, 4) + c(10, 20) give?

  • c(11, 22)
  • An error
  • c(11, 22, 13, 24)
  • c(11, 22, 33, 44)

Answer: c(11, 22, 13, 24). R recycles the shorter vector: 1+10, 2+20, 3+10, 4+20.

What does sum(x > 8000) compute?

  • The total of all values
  • The count of elements greater than 8000
  • The mean of x
  • The max of x

Answer: The count of elements greater than 8000. Comparing gives TRUE/FALSE; summing counts the TRUEs.

What does x[0] return in R?

  • The first element
  • An error
  • NULL
  • An empty vector

Answer: An empty vector. Index 0 selects nothing, returning a length-0 (empty) vector.