4 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Extract Specific Columns from Data Frame in R

How to Extract Specific Columns from Data Frame in R

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 extract specific columns from a data frame in R:

Method 1: Extract Specific Columns Using Base R

df[c('col1', 'col3', 'col4')]

Method 2: Extract Specific Columns Using dplyr

library(dplyr)

df %>%
  select(col1, 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', 'B', 'C', 'D', 'E'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28),
                 steals=c(9, 12, 4, 7, 8))

#view data frame
df

  team points assists rebounds steals
1    A     99      33       30      9
2    B     90      28       28     12
3    C     86      31       24      4
4    D     88      39       24      7
5    E     95      34       28      8

Method 1: Extract Specific Columns Using Base R

The following code shows how to extract the team, assists, and rebounds columns using base R:

#select 'team', 'assists' and 'rebounds' columns
df[c('team', 'assists', 'rebounds')]

  team assists rebounds
1    A      33       30
2    B      28       28
3    C      31       24
4    D      39       24
5    E      34       28

Notice that each of the columns we specified have been extracted from the data frame.

Also note that you can extract these columns by index position as well:

#select columns in index positions 1, 3 and 4
df[c(1, 3, 4)]

  team assists rebounds
1    A      33       30
2    B      28       28
3    C      31       24
4    D      39       24
5    E      34       28

This syntax extracts the columns in column index positions 1, 3 and 4.

Method 2: Extract Specific Columns Using dplyr

The following code shows how to extract the team, assists, and rebounds columns using the select() function from the dplyr package:

library(dplyr)

#select 'team', 'assists' and 'rebounds' columns
df %>%
  select(team, assists, rebounds)

  team assists rebounds
1    A      33       30
2    B      28       28
3    C      31       24
4    D      39       24
5    E      34       28

Notice that each of the columns we specified have been extracted from the data frame.

Also note that you can extract these columns by index position as well:

library(dplyr)

#select 'team', 'assists' and 'rebounds' columns
df %>%
  select(1, 3, 4)

  team assists rebounds
1    A      33       30
2    B      28       28
3    C      31       24
4    D      39       24
5    E      34       28

This syntax extracts the columns in column index positions 1, 3 and 4.

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