Extract Certain Columns of Data Frame in R (4 Examples)

 

This article explains how to extract specific columns of a data set in the R programming language.

I will show you four programming alternatives for the selection of data frame columns. More precisely, the tutorial will contain the following contents:

Let’s move on to the examples!

 

Creation of Example Data

In the examples of this tutorial, I’m going to use the following data frame:

data <- data.frame(x1 = c(2, 1, 5, 1),   # Create example data
                   x2 = c(7, 1, 1, 5),
                   x3 = c(9, 5, 4, 9),
                   x4 = c(3, 4, 1, 2))
data                                     # Print example data

 

example data frame

Table 1: Example Data Frame.

 

Our example data frame consists of four numeric columns and four rows.

In the following, I’m going to show you how to select certain columns from this data frame. I will show you four different alternatives, which will lead to the same output. It depends on your personal preferences, which of the alternatives suits you best.

 

Example 1: Subsetting Data by Column Name

The most common way to select some columns of a data frame is the specification of a character vector containing the names of the columns to extract. Consider the following R code:

data[ , c("x1", "x3")]                   # Subset by name

 

Subset of original example data frame

Table 2: Subset of Example Data Frame.

 

As you can see based on Table 2, the previous R syntax extracted the columns x1 and x3. The previous R syntax can be explained as follows:

  • First, we need to specify the name of our data set (i.e. data)
  • Then, we need to open some square brackets (i.e. [])
  • Within these brackets, we need to write a comma to reflect the two dimensions of our data. Everything before the comma selects specific rows; Everything behind the comma subsets certain columns.
  • Behind the comma, we specify a vector of character strings. Each element of this vector represents the name of a column of our data frame (i.e. x1 and x3).

That’s basically it. However, depending on your personal preferences and your specific data situation, you might prefer one of the other alternatives. So keep on reading…

 

Example 2: Subsetting Data by Column Position

A similar approach to Example one is the subsetting by the position of the columns. Consider the following R code:

data[ , c(1, 3)]                         # Subset by position

Similar to Example 1, we use square brackets and a vector behind the comma to select certain columns.

However, this time we are using a numeric vector, whereby each element of the vector stands for the position of the column.

The first column of our example data is called x1 and the column at the third position is called x3. For that reason, the previous R syntax would extract the columns x1 and x3 from our data set.

 

Example 3: Subsetting Data with select Argument of subset Function

In Example 3, we will extract certain columns with the subset function. Within the subset function, we need to specify the name of our data matrix (i.e. data) and the columns we want to select (i.e. x1 and x3):

subset(data, select = c("x1", "x3"))     # Subset with select argument

The output of the previous R syntax is the same as in Example 1 and 2.

 

Example 4: Subsetting Data with select Function (dplyr Package)

Many people like to use the tidyverse environment instead of base R, when it comes to data manipulation. A very popular package of the tidyverse, which also provides functions for the selection of certain columns, is the dplyr package. We can install and load the package as follows:

install.packages("dplyr")                # Install dplyr R package
library("dplyr")                         # Load dplyr R package

Now, we can use the %>% operator and the select function to subset our data set:

data %>% select(x1, x3)                  # Subset with select function

Again, the same output as in the previous examples. It’s up to you to decide, which option you like the most.

 

Video & Further Resources

There was a lot of content in this tutorial. However, if you need more explanations on the different approaches and functions, you could have a look at the following video of my YouTube channel. In the video, I’m explaining the examples of this tutorial in more detail:

 

 

In addition, you could have a look at the other R tutorials of my homepage. You can find some interesting tutorials for the manipulation of data sets in R below:

In this tutorial you learned how to extract specific columns of a data frame in the R programming language. If you have comments or questions, please let me know in the comments section below.

 



Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top