7 The Data Frame
Overview
Now that we are working with more than on variable, we need to consider how data sets are stored in R, which brings us to the “dataframe”, the R equivalent of a spreadsheet. We’ll be following Chapter 7 in Essential R Course Notes.
Objectives
Upon completion of this lesson, you should be able to:
- create a dataframe from vectors in your workspace,
- load a built-in data frame using the function
data()
, - query and change variables and values within dataframes,
- create new variables within data frames, and
- combine data frames.
Data and R Code Files
The R code file and data files for this lesson can be found on the Essential R - Notes on learning R page.
7.1 Lists and Data Frames
In this screencast, we will introduce the list, which is a very flexible R object, and then the dataframe, which is a special type of list. We’ll learn how to create a dataframe from variables in the workspace.
7.2 Accessing Data in Data Frames - Part I
Now that we have created a dataframe, we’ll see how to access values and variables within it, both by calling variables by name and by using the indexing operator []
to index a two-dimensional object.
7.3 Accessing Data in Data Frames - Part II
This is a continuation of the previous video.
7.4 Attaching Data Frames
Here we will investigate the function attach()
, and I will suggest that it should be used with caution (if at all) and with a full understanding of what it does.
7.5 Changing Data Frames
Here we will demonstrate how we change values within data frames and how new variables can be created within data frames.
7.6 Combining Data Frames
In this video, we’ll consider how dataframes can be combined using cbind()
to bind columns together.
7.7 Metadata: Adding Comments to Data Frames
In this short screencast, we’ll introduce the comment()
function, which retrieves or sets the comment attribute for dataframes and can be used to add comments to data.