R has some specialized syntax for accessing data structures. You can fetch a single item from a structure, or multiple items (possibly as a multidimensional array) using R’s index notation. You can fetch items by location within a data structure or by name.
Table 6-2 shows the operators in R used for accessing objects in a data structure.
Table 6-2. Data structure access notation
|Vectors, lists||Returns objects from object |
|Vectors, lists||Returns a single element of |
|Lists||Returns object with name |
|S4 objects||Returns element stored in object
Although the single-bracket notation and double-bracket notation look very similar, there are three important differences. First, double brackets always return a single element, while single brackets may return multiple elements. Second, when elements are referred to by name (as opposed to by index), single brackets match only named objects exactly, while double brackets allow partial matches. Finally, when used with lists, the single-bracket notation returns a list, but the double-bracket notation returns ...