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:


  1. create a dataframe from vectors in your workspace,
  2. load a built-in data frame using the function data(),
  3. query and change variables and values within dataframes,
  4. create new variables within data frames, and
  5. 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.

R logo

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.

Note! R will complain if the variables used to make a dataframe are not the same length.

Video - STAT 484 Lesson: 7.1

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.

Video - STAT 484 Lesson: 7.2

7.3 Accessing Data in Data Frames - Part II

This is a continuation of the previous video.

Video - STAT 484 Lesson: 7.3

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

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.

Video - STAT 484 Lesson: 7.4

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.

Video - STAT 484 Lesson: 7.5

7.6 Combining Data Frames

In this video, we’ll consider how dataframes can be combined using cbind() to bind columns together.

Video - STAT 484 Lesson: 7.6

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.

Video - STAT 484 Lesson: 7.7