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

R




# load the library
library(dplyr)
  
# create dataframe with 3 columns
# id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby','gnanesh',
                        'rohith','pinkey','dhanush',
                        'sravan','gnanesh','ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada',
                           'guntur','hyd','tenali','hyd'))
  
# select id column from the dataframe by 
# column name
print(select(data1,id))
    
# select name column from the dataframe by
# column name
print(select(data1,name))


Output:

Example 2: R program to select multiple columns

R




# load the library
library(dplyr)
  
# create dataframe with 3 columns 
# id,name and address
data1=data.frame(id=c(1,2,3,4,5,6,7,1,4,2),
                   
                 name=c('sravan','ojaswi','bobby','gnanesh',
                        'rohith','pinkey','dhanush',
                        'sravan','gnanesh','ojaswi'),
                   
                 address=c('hyd','hyd','ponnur','tenali',
                           'vijayawada','vijayawada',
                           'guntur','hyd','tenali','hyd'))
  
# select multiple columns from the dataframe
# by column name
print(select(data1,id,name,address))


Output:

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:

Similar Reads

Select column with column name

Here we will use select() method to select column by its name...

Select column(s) by position

...

Select column which contains a value or matches a pattern

...

Select column which starts with or ends with certain character

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

Select all columns

...