Learning Objectives

  • Extract values from vectors and data frames.
  • Perform operations on columns in a data frame.
  • Append columns to a data frame.
  • Create subsets of a data frame.

In this lesson you will learn how to extract and manipulate data stored in data frames in R. We will work with the E. coli metadata file that we used previously. Be sure to read this file into a dataframe named metadata, if you haven’t already done so.

Because the columns of a data frame are vectors, we will first learn how to extract elements from vectors and then learn how to apply this concept to select rows and columns from a data frame.

Extracting values with indexing and sequences


Let’s create a vector containing the first ten letters of the alphabet.

In order to extract one or several values from a vector, we must provide one or several indices in square brackets, just as we do in math. R indexes start at 1. Programming languages like Fortran, MATLAB, and R start counting at 1, because that’s what human beings typically do. Languages in the C family (including C++, Java, Perl, and Python) count from 0 because that’s simpler for computers to do.

So, to extract the 2nd element of ten_letters we type:

We can extract multiple elements at a time by specifying mulitple indices inside the square brackets as a vector. Notice how you can use : to make a vector of all integers two numbers.

Quick exercise / formative assessment: Select every other element in ten_letters.

What if we were dealing with a much longer vector? We can use the seq() function to quickly create sequences of numbers.


Fill in the blank to select the even elements of ten_letters using the seq() function.



ten_letters[seq(2, 10, by = 2)] {: .solution} {: .challenge}

Data frames

The metadata data frame has rows and columns (it has 2 dimensions), if we want to extract some specific data from it, we need to specify the “coordinates” we want from it. Row numbers come first, followed by column numbers (i.e. [row, column]).


The function nrow() on a data.frame returns the number of rows. For example, try typing nrow(metadata). Usenrow()andseq()to create a new data frame calledmeta_by_2that includes all even numbered rows ofmetadata`.


meta_data[seq(2, nrow(metadata), by = 2, ]

{: .solution} {: .challenge}

For larger datasets, it can be tricky to remember the column number that corresponds to a particular variable. Sometimes the column number for a particular variable can change if your analysis adds or removes columns. The best practice when working with columns in a data frame is to refer to them by name. This also makes your code easier to read and your intentions clearer.

There are two ways to select a column by name from a data frame:

  • Using dataframe[ , "column_name"]
  • Using dataframe$column_name

You can do operations on a particular column, by selecting it using the $ sign. In this case, the entire column is a vector. You can use names(metadata) or colnames(metadata) to remind yourself of the column names. For instance, to extract all the strain information from our datasets:

The first method allows you to select multiple columns at once. Suppose we wanted strain and clade information:

You can even access columns by column name and select specific rows of interest. For example, if we wanted the strain and clade of just rows 4 through 7, we could do:

<!– Still need to address the following learning objectives: * Append columns to a data frame. * Create subsets of a data frame.

The following headings are just suggestions.

Manipulating columns

Mathematical operations

Appending new columns

Creating subsets

Data Carpentry, 2017-2018. License. Contributing.
Questions? Feedback? Please file an issue on GitHub.
On Twitter: @datacarpentry