Vectors in R

Interactive R tutorial on vectors: create them with c(), index elements, run vectorized maths, and use seq() and rep() β€” all live in your browser.

A vector is a sequence of values β€” the fundamental building block of R. Edit and Run each box.

Watch: vectors in one minute

Every step below β€” creating, indexing and summarising vectors β€” run live in RStudio, then try each one in the consoles that follow.

Download the R script  Β·  β–Ά Practice in the playground β€” or edit and run each snippet below.

Create a vector with c()

c() (for β€œcombine”) glues values into a vector:

Vectors can hold text too:

Maths happens on every element at once

This is R’s superpower β€” no loops needed:

Pick out elements with [ ]

Square brackets select by position (R counts from 1):

That last line β€” selecting by a condition β€” is used constantly in real analysis.

Handy ways to build vectors

Summarise a vector

Your turn


Next: Data types β†’ β€” the different kinds of values R stores.