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.

Additional Resources

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

How to Select Only Numeric Columns in R
How to Delete Multiple Columns in R
How to Reorder Columns in R

Leave a Reply

Your email address will not be published.