如何在 R 中按名称选择多个 DataFrame 列?
在本文中,我们将讨论如何在 R 编程语言中按名称从 DataFrame 中选择多个列。为了获得多列,我们将使用列表数据结构。通过使用列表,我们可以传递用逗号分隔的数据框列。然后,我们可以使用 list()函数获取列表
语法:
list(dataframe_name$column1,dataframe_name$column2,.,dataframe_name$column n)
示例 1:
R
# create a vector with student id
vector1=c(7058,7059,7075,7076)
# create a vector with student name
vector2=c("Sravan kumar","Jyothika",
"Deepika","Kyathi")
# create a vector with student address
vector3=c("ponnur","tenali","repalle",
"ponnur")
# pass these vectors to the dataframe
dataframe1=data.frame(id=vector1,names=vector2,
address=vector3)
print(dataframe1)
# select multiple columns from the
# dataframe1 using list datastructure
a=list(dataframe1$id,dataframe1$names,dataframe1$address)
print(a)
R
# create a vector with student id
vector1=c(7058,7059,7075,7076)
# create a vector with student name
vector2=c("Sravan kumar","Jyothika",
"Deepika","Kyathi")
# create a vector with student address
vector3=c("ponnur","tenali","repalle","ponnur")
# pass these vectors to the dataframe
dataframe1=data.frame(id=vector1,names=vector2,
address=vector3)
print(dataframe1)
# select multiple columns from the
# dataframe1 using list datastructure
a=list(dataframe1$id,dataframe1$names)
print(a)
输出:
示例 2 :
电阻
# create a vector with student id
vector1=c(7058,7059,7075,7076)
# create a vector with student name
vector2=c("Sravan kumar","Jyothika",
"Deepika","Kyathi")
# create a vector with student address
vector3=c("ponnur","tenali","repalle","ponnur")
# pass these vectors to the dataframe
dataframe1=data.frame(id=vector1,names=vector2,
address=vector3)
print(dataframe1)
# select multiple columns from the
# dataframe1 using list datastructure
a=list(dataframe1$id,dataframe1$names)
print(a)
输出: