Skip to main content

4.1) Manipulating Data


It’s important to understand indexing of data objects in R. This is where square brackets [ ] are used. Careful, systematic thinking here will be rewarded!

Indexing vectors

If you want to specify one or more elements of a vector, use single square brackets around the number of the element/s.

Try:
Seq <- 10:1
Seq[2]; Seq[c(1,3,4)]

In this way you can also reorder a vector; e.g. Seq[10:1]

Or you can specify the elements that meet a logical condition by putting the name of the vector followed by the condition into the square brackets; e.g. Seq[Seq <= 5]

How did that work? Check what the bit inside the square brackets does on its own.