Intro to Vectors in R

Student Information

Welcome

A vector is R’s basic container for a sequence of values of the same type. This short tutorial covers creating vectors, indexing them, and a couple of summary functions.

Creating vectors

The c() function (“combine”) builds a vector out of individual values:

You can pull out one element with [ ] and a position (R counts from 1, not 0):

Exercise: total and average

The sum() function adds up every element of a vector: sum(scores).

sum(scores)

The mean() function averages every element of a vector: mean(scores).

mean(scores)

Quick check

Your Answers