How can I extract specific columns from a data frame in R?

How can I extract specific columns from a data frame in R?

Extracting specific columns from a data frame in R refers to the process of selecting and obtaining only the desired columns from a larger dataset. This can be achieved by using the “subset” function or by using the column names or indices of the desired columns. This allows for easier manipulation and analysis of the data, as well as reducing the amount of unnecessary information. This feature is particularly useful in data analysis and visualization tasks, as it allows for more focused and efficient data handling.

Extract Specific Columns from Data Frame in R


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

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.

Cite this article

stats writer (2024). How can I extract specific columns from a data frame in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-extract-specific-columns-from-a-data-frame-in-r/

stats writer. "How can I extract specific columns from a data frame in R?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-extract-specific-columns-from-a-data-frame-in-r/.

stats writer. "How can I extract specific columns from a data frame in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-extract-specific-columns-from-a-data-frame-in-r/.

stats writer (2024) 'How can I extract specific columns from a data frame in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-extract-specific-columns-from-a-data-frame-in-r/.

[1] stats writer, "How can I extract specific columns from a data frame in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I extract specific columns from a data frame in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top