Lesson 7: The Data FrameLesson 7: The Data Frame
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.
- Create a dataframe from vectors in your workspace
- Load a built-in data frame using the function
- Query and change variables and values within dataframes
- Create new variables within data frames
- 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 Frames7.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 i7.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 calling variables by name and using the indexing operator "
" to index a 2-dimansional object.
7.3: Accessing Data in Data Frames - Part ii7.3: Accessing Data in Data Frames - Part ii
This 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 Frames7.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 Frames7.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 Frames7.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 Frames7.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.