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.1 Identifying object structure
Given an object, the best way to understand what data structure it represents is to use the structure function str()
. str()
stands for structure and provides a compact display of the internal structure of an R object.
# different data structures
vector <- 1:10
list <- list(item1 = 1:10, item2 = LETTERS[1:18])
matrix <- matrix(1:12, nrow = 4)
df <- data.frame(item1 = 1:18, item2 = LETTERS[1:18])
# identify the structure of each object
str(vector)
int [1:10] 1 2 3 4 5 6 7 8 9 10
str(list)
List of 2
$ item1: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ item2: chr [1:18] "A" "B" "C" "D" ...
str(matrix)
int [1:4, 1:3] 1 2 3 4 5 6 7 8 9 10 ...
str(df)
'data.frame': 18 obs. of 2 variables:
$ item1: int 1 2 3 4 5 6 7 8 9 10 ...
$ item2: Factor w/ 18 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10 ...
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.
# assess attributes of an object
attributes(df)
$names
[1] "item1" "item2"
$class
[1] "data.frame"
$row.names
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
attributes(matrix)
$dim
[1] 4 3
# assess names of an object
names(df)
[1] "item1" "item2"
# assess the dimensions of an object
dim(matrix)
[1] 4 3
# assess the class of an object
class(list)
[1] "list"
# access the length of an object
length(vector)
[1] 10
# note that length will measure the number of items in
# a list or number of columns in a data frame
length(list)
[1] 2
length(df)
[1] 2
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.