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

How to Select Specific Columns in R dataframe?

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

Similar Reads

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

Selecting specific Columns Using Base R by column index

...

Selecting specific columns by subsetting data by column name

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

Selecting specific columns by subsetting data by column index

...

Selecting specific columns using dplyr package 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...

Selecting specific columns using dplyr package by column index

...

Contact Us