How to select multiple DataFrame columns by name in R ?
In this article, we will discuss how to select multiple columns from a DataFrame by name in R Programming Language. To get multiple columns we will use the list data structure. By using a list we can pass the dataframe columns separated with a comma. Then, we can get list by using list() function
Syntax:
list(dataframe_name$column1,dataframe_name$column2,.,dataframe_name$column n)
Example1:
R
# create a vector with student idvector1=c(7058,7059,7075,7076) # create a vector with student namevector2=c("Sravan kumar","Jyothika", "Deepika","Kyathi") # create a vector with student addressvector3=c("ponnur","tenali","repalle", "ponnur") # pass these vectors to the dataframedataframe1=data.frame(id=vector1,names=vector2, address=vector3) print(dataframe1) # select multiple columns from the# dataframe1 using list datastructurea=list(dataframe1$id,dataframe1$names,dataframe1$address) print(a) |
Output:
Example 2:
R
# create a vector with student idvector1=c(7058,7059,7075,7076) # create a vector with student namevector2=c("Sravan kumar","Jyothika", "Deepika","Kyathi") # create a vector with student addressvector3=c("ponnur","tenali","repalle","ponnur") # pass these vectors to the dataframedataframe1=data.frame(id=vector1,names=vector2, address=vector3) print(dataframe1) # select multiple columns from the# dataframe1 using list datastructurea=list(dataframe1$id,dataframe1$names) print(a) |
Output:


