Lesson 7: The Data Frame
Lesson 7: The Data FrameOverview
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.
Objectives
- 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
- Combine data frames
R
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
7.1 - Lists and Data FramesIn 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
7.2 - Accessing Data in Data Frames - Part iNow that we have created a dataframe we'll see how to access values and variables within it, both calling variables by name and using the indexing operator "[]
" to index a 2-dimansional object.
7.3: Accessing Data in Data Frames - Part ii
7.3: Accessing Data in Data Frames - Part iiThis is a continuation of the previous video.
Note: We should mention the very useful function subset()
which allows us to extract subsets from data frames based on multiple logical tests. For example subset(cars, hp<150 & cyl<8)
. It is worth noting that the dataframe created by subset()
will contain all factor levels of the original dataframe, even if some are not used - droplevels()
can be used to remove unused levels.
7.4 - Attaching Data Frames
7.4 - Attaching Data FramesHere 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
7.5 - Changing Data FramesHere 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
7.6 - Combining Data FramesIn this video we'll consider how dataframes can be combined using cbind()
to bind columns together.
7.7 - Metadata: Adding Comments to Data Frames
7.7 - Metadata: Adding Comments to Data FramesIn 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.