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:
Contact Us