Skip to content
Learn Netverks

Lesson

Step 8/36 22% through track

vectors-r

Vectors

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Vectors: the syntax, patterns, and safety habits you need before advancing in R.

Vectors are atomic—everything in R is vectorized; factors encode categorical variables for modeling.

You will apply Vectors in contexts like: Research pipelines, Shiny dashboards, and statistical reporting.

Write R in the editor and click Run on server—the dev runner executes with Rscript; use print() or cat() and base R in playground snippets (tidyverse locally; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

The vector is R's fundamental data structure—atomic vectors hold elements of one type. Most functions are vectorized: operations apply element-wise without explicit loops.

Creating vectors

nums <- c(10, 20, 30)
chars <- c("a", "b", "c")
seq_vec <- seq(1, 5, by = 1)
rep_vec <- rep(1, times = 3)

Vectorized math and recycling

x <- c(1, 2, 3)
print(x * 2)
print(x + c(10, 20, 30))

Shorter vectors recycle in binary ops—watch for silent length mismatches (R warns when lengths are not multiples).

Indexing (1-based)

vals <- c(10, 20, 30, 40)
print(vals[1])
print(vals[c(1, 3)])

R indexes from 1, unlike Python's 0—off-by-one bugs are a common interview topic.

Important interview questions and answers

  1. Q: What does c() do?
    A: Combines values into a vector—the c stands for combine/concatenate.
  2. Q: Vectorization benefit?
    A: Write x * 2 instead of looping—faster and clearer for numeric work.

Self-check

  1. What index is the first element in R?
  2. What function creates a sequence from 1 to 5?

Tip: R indexes from 1—off-by-one vs Python is a frequent bug in interviews.

Interview prep

Why vectorization?

Element-wise ops are faster and clearer than manual loops for numeric work.

1-based indexing?

First element is x[1], unlike zero-based languages like Python.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Recycling rules?
  • c() combine?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump