Open In App

How to Select Specific Columns in R dataframe?

Last Updated : 21 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to select specific columns from a data frame in the R Programming Language.

Selecting specific Columns Using Base R by column name

In this approach to select a specific column, the user needs to write the name of the column name in the square bracket with the name of the given data frame as per the requirement to get those specific columns needed by the user.

Syntax:

data_frame

R




# Creating DataFrame
gfg <- data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9),
                   b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3),
                   c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7),
                   d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7),
                   e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4))
 
# Selecting specific Columns Using Base
# R by column name
gfg[c('b', 'd', 'e')]


Output:

   b d e
1 1 4 3
2 8 6 1
3 6 8 6
4 8 4 4
5 6 6 8
6 7 4 9
7 4 8 7
8 1 9 8
9 7 8 9
10 3 7 4

Selecting specific Columns Using Base R by column index

In this approach to select the specific columns, the user needs to use the square brackets with the data frame given, and. With it, the user also needs to use the index of columns inside of the square bracket where the indexing starts with 1, and as per the requirements of the user has to give the required column index to inside the brackets 

Syntax:

data_frame

R




# Creating DataFrame
gfg < - data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9),
                   b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3),
                   c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7),
                   d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7),
                   e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4))
 
# Selecting specific Columns Using Base R
# by column index
gfg[c(2, 4, 5)]


Output:

   b d e
1 1 4 3
2 8 6 1
3 6 8 6
4 8 4 4
5 6 6 8
6 7 4 9
7 4 8 7
8 1 9 8
9 7 8 9
10 3 7 4

Selecting specific columns by subsetting data by column name

In this method of selecting specific columns by subsetting data, the user needs to do the specification of a character vector containing the names of the columns to extract, the user has to enter the vector of the characters which corresponds to the column name in the square bracket with the data frame 

Syntax:

data_frame[,c(column_name_1,column_name_2,...)]

R




# Creating DataFrame
gfg < - data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9),
                   b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3),
                   c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7),
                   d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7),
                   e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4))
 
# Selecting specific columns by subsetting
# data by column name
gfg[, c('b', 'd', 'e')]


Output:

   b d e
1 1 4 3
2 8 6 1
3 6 8 6
4 8 4 4
5 6 6 8
6 7 4 9
7 4 8 7
8 1 9 8
9 7 8 9
10 3 7 4

Selecting specific columns by subsetting data by column index

In this method of selecting specific columns by subsetting data, the user needs to do the specification of an integer vector containing the index of the columns to extract, the user has to enter the vector of the indexes which corresponds to the column index in the square bracket with the data frame

Syntax:

data_frame[,c(column_index_1,column_index_2,...)]

R




# Creating DataFrame
gfg < - data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9),
                   b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3),
                   c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7),
                   d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7),
                   e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4))
 
# Selecting specific columns by subsetting data
# by column index:
gfg[, c(2, 4, 5)]


Output:

   b d e
1 1 4 3
2 8 6 1
3 6 8 6
4 8 4 4
5 6 6 8
6 7 4 9
7 4 8 7
8 1 9 8
9 7 8 9
10 3 7 4

Selecting specific columns by Subsetting Data with select Argument of subset Function

Subset function: This function will be returning the subsets of data frames that meet conditions.

Syntax:

subset(x, subset, select, drop = FALSE, …)

Parameters:

  • x: object to be subsetted.
  • subset: logical expression indicating elements or rows to keep: missing values are taken as false.
  • select: expression, indicating columns to select from a data frame.
  • drop: passed on to [ indexing operator.
  • …: further arguments to be passed to or from other methods.

R




# Creating DataFrame
gfg <- data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9),
                   b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3),
                   c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7),
                   d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7),
                   e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4))
 
# Selecting specific columns by Subsetting
# Data with select Argument of subset Function
subset(gfg, select=c('b', 'd', 'e'))


Output:

   b d e
1 1 4 3
2 8 6 1
3 6 8 6
4 8 4 4
5 6 6 8
6 7 4 9
7 4 8 7
8 1 9 8
9 7 8 9
10 3 7 4

Selecting specific columns using dplyr package by column name

In this approach to select the specific columns of the given data frame, the user needs first install and import the dplyr package in the working R console of the user and then call the select function and pass the name of the required columns as the argument of this function

Syntax:

data_frame %>% select(column_name_1,column_name_2,...)   

R




# Importing dplyr library
library("dplyr")
 
# Creating DataFrame
gfg < - data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9),
                   b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3),
                   c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7),
                   d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7),
                   e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4))
 
# Selecting specific columns using dplyr
# package by column name
gfg %>% select(b, d, e)


Output:

   b d e
1 1 4 3
2 8 6 1
3 6 8 6
4 8 4 4
5 6 6 8
6 7 4 9
7 4 8 7
8 1 9 8
9 7 8 9
10 3 7 4

Selecting specific columns using dplyr package by column index

In this approach to select the specific columns of the given data frame, the user needs first install and import the dplyr package in the working R console of the user and then call the select function and pass the index of the required columns as the argument of this function

Syntax:

data_frame %>% select(column_index_1,column_index_2,...)  

R




# Importing dplyr library
library("dplyr")
 
# Creating DataFrame
gfg < - data.frame(a=c(5, 1, 1, 5, 6, 7, 5, 4, 7, 9),
                   b=c(1, 8, 6, 8, 6, 7, 4, 1, 7, 3),
                   c=c(7, 1, 8, 9, 4, 1, 5, 6, 3, 7),
                   d=c(4, 6, 8, 4, 6, 4, 8, 9, 8, 7),
                   e=c(3, 1, 6, 4, 8, 9, 7, 8, 9, 4))
 
# Selecting specific columns using dplyr
# package by column index
gfg %>% select(2, 4, 5)


Output:

   b d e
1 1 4 3
2 8 6 1
3 6 8 6
4 8 4 4
5 6 6 8
6 7 4 9
7 4 8 7
8 1 9 8
9 7 8 9
10 3 7 4


Similar Reads

Choose Specific Columns of a Data Frame in R Programming - select() Function
select() function in R Language is used to choose whether a column of the data frame is selected or not. Syntax: select(x, expr) Parameters: x: Data frame expr: condition for selection Example 1: # R program to select specific columns # Loading library library(dplyr) # Create a data frame d &lt;- data.frame( name = c(&quot;Abhi&quot;, &quot;Bhavesh
2 min read
Read CSV file and select specific rows and columns in R
In this article, we are going to see how to read CSV file and select specific rows and columns in R Programming Language. CSV file: To import a CSV file into the R environment we need to use a pre-defined function called read.csv(). Pass filename.csv as a parameter within quotations. First, we need to set the path to where the CSV file is located u
1 min read
How to read Excel file and select specific rows and columns in R?
In this article, we will discuss how to read an Excel file and select specific rows and columns from it using R Programming Language. File Used: To read an Excel file into R we have to pass its path as an argument to read_excel() function readxl library. Syntax: read_excel(path) To select a specific column we can use indexing. Syntax: df [ row_inde
2 min read
Use apply Function Only for Specific DataFrame Columns in R
In this article, we are going to apply functions only for specific dataframe columns in the R language. The function in R can be specified using the function keyword, which takes the element x, for instance, as input and modifies each of the elements in x according to the user-defined function. fun &lt;- function(x){ } The pre-defined or user-defin
3 min read
How to select multiple DataFrame columns by name in R ?
In this article, we will discuss how to select multiple columns from a DataFrame by name in R Programming Language. To get multiple columns we will use the list data structure. By using a list we can pass the dataframe columns separated with a comma. Then, we can get list by using list() function Syntax: list(dataframe_name$column1,dataframe_name$c
1 min read
Select Only Numeric Columns from DataFrame in R
In this article, we will discuss how to select only numeric columns from dataframe in R Programming Language. Method 1: Using Dplyr package We can use select_if() function to get numeric columns by calling the function with the dataframe name and isnumeric() function that will check for numeric columns. Syntax: select_if(dataframe, is.numeric) wher
2 min read
Select Odd and Even Rows and Columns from DataFrame in R
In this article, we will discuss how to select odd and even rows from a dataframe in R programming language. Getting Odd Rows from the Data Frame The number of rows in a data frame in R can be fetched by the nrow() method. It returns the number of rows in the data frame. The seq_len() method is then applied to generate the integers beginning with 1
6 min read
How to Select DataFrame Columns by Index in R?
In this article, we will discuss how to select columns by index from a dataframe in R programming language. Note: The indexing of the columns in the R programming language always starts from 1. Method 1: Select Specific Columns By Index with Base R Here, we are going to select columns by using index with the base R in the dataframe. Syntax: datafra
2 min read
Select variables (columns) in R using Dplyr
In this article, we are going to select variables or columns in R programming language using dplyr library. Dataset in use: Select column with column name Here we will use select() method to select column by its name Syntax: select(dataframe,column1,column2,.,column n) Here, data frame is the input dataframe and columns are the columns in the dataf
5 min read
Select Subset of DataTable Columns in R
In this article, we will discuss how to select a subset of data table columns in R programming language. Let's create a data table using a matrix. First, we need to load data.table package in the working space. Installation install.packages("data.table") Loading library("data.table") Dataset in use: Method 1: Using [] We can select a subset of data
2 min read