VOOZH about

URL: https://www.geeksforgeeks.org/r-language/select-variables-columns-in-r-using-dplyr/

⇱ Select variables (columns) in R using Dplyr - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Select variables (columns) in R using Dplyr

Last Updated : 21 Jul, 2021

In this article, we are going to select variables or columns in R programming language using dplyr library.

Dataset in use:

👁 Image

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 dataframe to be displayed

Example 1: R program to select columns

Output:

👁 Image

Example 2: R program to select multiple columns

Output:

👁 Image

Select column(s) by position

We can also use the column position and get the column using select() method. Position starts with 1.

Syntax:

select(dataframe,column1_position,column2_position,.,column n_position)

where, dataframe is the input dataframe and column position is an column number

For selecting multiple columns we can use range operator ";" to select columns by their position

Syntax:

select(dataframe,start_position:end_position)

where, dataframe is the input dataframe, start_position  is a column number starting position and end_position  is a column number ending position

Example 1: R program to select particular column by column position

Output:

👁 Image

Example 2: R program to select multiple columns by positions

Output:

👁 Image

Example 3: R program to select multiple columns by position with range operator

Output:

👁 Image

Select column which contains a value or matches a pattern

Here, we will display the column values based on values or pattern present in the column 

Method 1: Using contains() 

Display the column that contains the given sub string

Syntax:

select(dataframe,contains('sub_string'))

Here, dataframe is the input dataframe and sub_string is the string present in the column name

Example: R program to select column based on substring

Output:

👁 Image

Method 2: Using matches()

It will check and display the column that contains the given sub string

select(dataframe,matches('sub_string'))

Here, dataframe is the input dataframe and sub_string is the string present in the column name

Example: R program to select column based on substring

Output:

👁 Image

Select column which starts with or ends with certain character

Here we can also select columns based on starting and ending characters.

  • starts_with() is used to return the column that starts with the given character.

Syntax:

select(dataframe,starts_with('substring'))

Where, dataframe is the input dataframe and substring is the character/string that starts with it

  • ends_with() is used to return the column that ends with the given character.

Syntax:

select(dataframe,ends_with('substring'))

where, dataframe is the input dataframe and substring is the character/string that ends with it

Example 1: R program to display columns that starts with a character/substring

Output:

👁 Image

Example 2: R program to select column that ends with a given string or character

Output:

👁 Image

Select all columns

We can select all the columns in the data frame by using everything() method.

Syntax:

select(dataframe,everything())

Example: R program to select all columns

Output:

👁 Image
Comment
Article Tags:
Article Tags:

Explore