5.1 Structure and attributes

The following sections illustrate how to create, manipulate, and subset each of these data structures. However, it’s benficial to first understand two components of that apply to all data structures - structure and attributes.

5.1.2 Understanding object attributes

R objects can have attributes, which are like metadata for the object. These metadata can be very useful in that they help to describe the object. For example, column names on a data frame help to tell us what data are contained in each of the columns. Some examples of R object attributes are:

  • names, dimnames
  • dimensions (e.g. matrices, arrays)
  • class (e.g. integer, numeric)
  • length
  • other user-defined attributes/metadata

Attributes of an object (if any) can be accessed using the attributes() function. Not all R objects contain attributes, in which case the attributes() function returns NULL.

The following sections provide more details on how to take advantage of an object’s structure as well as how to view and create attributes for each type of object.