How to Select Specific Columns in R dataframe?

28 Nov, 2021
In this article, we will discuss how to select specific columns from dataframe in the R programming language.
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
Example:
R
# Creating DataFramegfg < - 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 namegfg[c('b', 'd', 'e')]
Output:
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
Example:
R
# Creating DataFramegfg < - 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 indexgfg[c(2, 4, 5)]
Output:
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,...)]
Example:
R
# Creating DataFramegfg < - 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 namegfg[, c('b', 'd', 'e')]
Output:
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,...)]
Example:
R
# Creating DataFramegfg < - 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:
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.
Example:
R
# Creating DataFramegfg < - 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 Functionsubset(gfg, select=c('b', 'd', 'e'))
Output:
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,...)   
Example:
R
# Importing dplyr librarylibrary("dplyr")  # Creating DataFramegfg < - 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 namegfg % > % select(b, d, e)
Output:
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,...)  
Example:
R
# Importing dplyr librarylibrary("dplyr")  # Creating DataFramegfg < - 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 indexgfg % > % select(2, 4, 5)
Output:
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to filter R DataFrame by values in a column?
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
Merge DataFrames by Column Names in R
How to Sort a DataFrame in R ?