# 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``` 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` 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:

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

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 have learned how to extract specific columns of a data frame in the R programming language. I have shown in multiple examples how to create subsets of consecutive and non-consecutive variables. If you have comments or questions, please let me know in the comments section below.

Subscribe to the Statistics Globe Newsletter

• Kashyap
November 17, 2020 5:44 am

If my column is in sequence like
X1 X2 X3 X4 X5

But i wat to output only X4 and X2 column in sequence of first X4 column and after X2 column.

How can i do that?

• Hey Kashyap,

You may use the following R code:

`data[ , c("x4", "x2")]`

Regards,

Joachim

• shubhangi
February 8, 2021 1:08 pm

what if i want to extract selected columns with specific row value?

• Rogue
March 19, 2021 10:48 pm

Hi, I’m trying to extract columns from multiple datasets so I can sum them but the number of columns in each dataset varies. I’m attempting to use a for loop.

Here is my attempt:

for (df in 1:length(locs)){
newdf 0] #get rid of all columns that have only 0s
newdfsum <- colSums(newdf[ , 9:length(newdf) ]) #sum everything in column 9 and after
summarysums[i] <- newdfsum #put new df or list in empty vector
}

I can do this for one, but I haven't been able to loop through multiple datasets..
Thank you!

• Hey,

I have just responded to your email.

Regards,

Joachim

• Abby
April 23, 2021 6:30 pm

how do you filter and pick specific rows to use

• William
June 17, 2021 1:27 pm

Hello. If I had a dataframe called df (containing 5 columns and 30 rows). What code would I use to subset rows 10 to 20 and columns 1 and 5 using base R?

• Hey William,

You may use the following R code:

`df_new <- df[10:20, c(1, 5)]`

Regards

Joachim

• William
June 23, 2021 8:23 am

Thank you, I appreciate it!

– William

• 