16.2 C
London
Thursday, July 24, 2025
HomeStatistics TutorialRHow to Keep Certain Columns in R (With Examples)

How to Keep Certain Columns in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories