list_1 <- list(10, 30, 50)
10 Data Structures (Part I)
10.1 Questions
List the data structures that can be used in R?
How do we define the various data structures?
What operations can be performed on the different data structures?
How do these data structures differ from one another in terms of storage and functionality?
10.2 Learning Objectives
Learn about data structures in R, specifically vectors, lists, and dataframes.
Define and manipulate vectors, lists, and dataframes.
Perform common operations on each data structure.
Identify and differentiate between the diverse data structures offered by R.
Choose the appropriate data structure based on the type and organization of your data.
Understand the strengths and limitations of each structure for efficient analysis.
10.3 Lesson Content
10.3.1 Introduction
Data structures in R are the formats used to organize, process, retrieve, and store data. They help to organize stored data in a way that the data can be used more effectively. Data structures vary according to the number of dimensions and the data types (heterogeneous or homogeneous) contained.
The primary data structures are:
- Vectors
- Lists
- Dataframes
- Matrices
- Arrays
- Factors
In this lesson, we will review the 1st set of data structures: vectors, lists, and dataframes.
10.3.2 Vectors
Discussed in the previous chapter on “Vectors” (sec-vectors)
10.3.3 Lists
Lists are objects/containers that hold elements of the same or different types. They can contain strings, numbers, vectors, dataframes, matrices, functions, or other lists. Lists are created with the list() function.
Examples:
- Three element list
- Single element list
- Three element list
- List with elements of different types
- List which contains a list
- Set names for the list elements
names(list_5)
names(list_5)
- Access elements within a list
Return a list within a list
list_5[1]
list_5[[1]]
Return an individual element within a list of lists
list_5[[1]][1]
list_5[["character vector"]]
- Length of list
length(list_1)
length(list_5)
Flatten out into a single vector using unlist()
unlist(list_5)
10.3.4 Dataframes
A data frame is one of the most common data objects used to store tabular data in R. Tabular data has rows representing observations and columns representing variables. Dataframes contain lists of equal-length vectors. Each column holds a different type of data, but within each column, the elements must be of the same type. The most common data frame characteristics are listed below:
- Columns should have a name,
- Row names should be unique,
- Various data can be stored (such as numeric, factor, and character), and,
- The individual columns should contain the same number of data items.
NOTE: Tibbles are the modern versions of dataframes, that have improved printing and subsetting features. The tibble package is a main component of the tidyverse; however, a review of the tidyverse is beyond the scope of this book and will be discussed in subsequent lessons.
10.3.4.1 Creation of data frames
df_1 <- data.frame(level, language, age)
Use stringsAsFactors = FALSE
to prevent R from converting string columns to factors.
df_2 <- data.frame(level, language, age, stringsAsFactors = FALSE)
10.3.4.2 Functions used to manipulate data frames
- Number of rows
nrow(df_1)
- Number of columns
ncol(df_1)
- Dimensions
dim(df_1)
- Class of data frame
class(df_1)
- Column names
colnames(df_1)
- Row names
rownames(df_1)
- Top and bottom values
head(df_1, n=2)
tail(df_1, n=2)
- Access columns / subset
df_1$level
- Access individual elements / subset
df_1[3,2]
df_1[2, 1:2]
- Access columns with index
df_1[, 3]
df_1[, c("language")]
- Access rows with index
df_1[2, ]
NOTE: To view the internal structure of various data types described above, the learner can use the str()
function.
Example
str(list_3)
str(df_1)
10.4 Exercises
Explain the characteristics and use cases of lists in R.
Create a list containing a numeric vector, character vector, and logical vector.
How can you add elements to a list in R?
Explain what a data frame is and why it is widely used in R.
Write code to create a data frame with columns for “Name,” “Age,” and “Gender” for three individuals.
How can you combine two data frames horizontally in R?
Discuss various methods for subsetting elements in data structures.
How do you check the structure of a data frame? What function is used?
How do you access a specific column of a data frame?
What are rownames and colnames in a data frame? How are they useful?
How do you add a new column to an existing data frame?
10.5 Summary
We have now covered half of the key data structures used in R. Knowledge of how to identify and manipulate vectors, lists, and dataframes will help the learner perform numerous computational tasks in R. Next, to complete our review of data structures, we will focus on matrices, arrays, and factors.