i

R Programming Complete Tutorial

Access List elements (List Slicing)

We are starting with list slicing. It will retrieve a slice of list elements, and for this, we will use a single square bracket "[ ]" operator.

The first example only returns the second element of the list. If we want to retrieve a series of elements, suppose second to fourth, we have to use a colon in between numbers. If our goal is to extract only the second and fourth elements, we use a vector containing 2 and 4 inside the single square bracket.

Member referencing:

Another way of accessing the list is member referencing. In order to reference a list member, we use a double square bracket operator.

In this first example, it will return the 2nd element, which is v2. If we want to access the first element of vector V2, first we need to point to the second vector using a double square, then the first member of that element will be selected using a single square. 

In the previous section, we have learned how to access a list using the name. We can even retrieve the first member of the second vector using member reference of the name of the second vector.