# How to Keep Certain Columns in R (With Examples)

You can use the following methods to only keep certain columns in a data frame in R:

Method 1: Specify Columns to Keep

#only keep columns 'col1' and 'col2'
new_df = subset(df, select = c(col1, col2))

Method 2: Specify Columns to Drop

#drop columns 'col3' and 'col4'
new_df = subset(df, select = c(col3, col4))

The following examples show how to use each method with the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'A', 'A', 'B', 'B', 'B'),
points=c(19, 14, 14, 29, 25, 30),
assists=c(4, 5, 5, 4, 12, 10),
rebounds=c(9, 7, 7, 6, 10, 11))

#view data frame
df

team points assists rebounds
1    A     19       4        9
2    A     14       5        7
3    A     14       5        7
4    B     29       4        6
5    B     25      12       10
6    B     30      10       11

## Method 1: Specify Columns to Keep

The following code shows how to define a new data frame that only keeps the “team” and “assists” columns:

#keep 'team' and 'assists' columns
new_df = subset(df, select = c(team, assists))

#view new data frame
new_df

team assists
1    A       4
2    A       5
3    A       5
4    B       4
5    B      12
6    B      10

The resulting data frame only keeps the two columns that we specified.

## Method 2: Specify Columns to Drop

The following code shows how to define a new data frame that drops the “team” and “assists” columns from the original data frame:

#drop 'team' and 'assists' columns
new_df = subset(df, select = -c(team, assists))

#view new data frame
new_df

points rebounds
1     19        9
2     14        7
3     14        7
4     29        6
5     25       10
6     30       11

The resulting data frame drops the “team” and “assists” columns from the original data frame and keeps the remaining columns.

The following tutorials explain how to perform other common tasks in R:

## 2 Replies to “How to Keep Certain Columns in R (With Examples)”

1. Jeff says:

Method 2 at the top of the page is missing a minus sign. It should be:
new_df = subset(df, select = -c(col3, col4))

2. NDTaco says:

The example code below the first of two sections titled “Method 2: Specify Columns to Drop” reads as follows:

#drop columns ‘col3’ and ‘col4’
new_df = subset(df, select = c(col3, col4))

You are missing a minus (-) sign before the c(col3, col4). The minus sign DOES appear in the example code below the second of the two sections titled “Method 2: Specify Columns to Drop,” so its not too big of a deal; however, as this difference is the only thing that is being explained in this topic, the minus sign is integral here.