Prepare Data: Indexing

Learn how to index observations in R.

It’s important to understand how R refers to the observations in a variable or the observations and variables in a dataset. In this lesson, we learn about indexing, which is the most basic reference method in R. We rely on this referencing or indexing method throughout the course. In the indexing method, we place the position values or identifying conditions inside a pair of square brackets [] to reference entries in variables or data frames.

Referencing observation in variable

We will start with how to reference an observation in a variable. For illustration, we use the population variable in dataset pwt7, denoted as pwt7$POP, as an example. As shown in figure,in the variable’s fifth observation equals 8728.408. We may refer to the fifth observation of pwt7$POP in two alternative methods, via either its position value or other unique identifying conditions.

Get hands-on with 1400+ tech skills courses.