📌  相关文章
📜  如何从数据框中提取列作为 R 中的向量?

📅  最后修改于: 2022-05-13 01:54:24.134000             🧑  作者: Mango

如何从数据框中提取列作为 R 中的向量?

在本文中,我们将在 R 编程语言中将数据帧列转换为向量。

脚步 -

  • 创建向量
  • 通过传递这些向量创建一个数据框
  • 使用“[[]]”运算符(索引)转换数据框列。 [[]] 用于访问数据框列。它用于索引数据框。

句法:

  • 显示向量

示例 1:

R
# creating a vector for names
names=c('siva nagulu','bhavanarayana','sireesha',
        'priyank chowdary','sridevi','ravi kumar')
  
# creating a vector for ages
ages=c(40,49,38,42,36,39)
  
#create a dataframe using these vectors
dataframe=data.frame(names,ages)
print(dataframe)
  
# converting ages dataframe column into vector
# by passing as index
my_data = dataframe[['names']]
print("--------convert into vector of names column-------")
print(my_data)
  
my_data = dataframe[['ages']]
print("--------convert into vector of ages column-------")
print(my_data)


R
# creating a vector for city
city=c('kakumanu','hyd','noida')
  
# create a dataframe using this vector
dataframe=data.frame(city)
print(dataframe)
  
# converting city dataframe column into vector
# by passing as index
my_data = dataframe[['city']]
print("--------convert into vector of city column-------")
print(my_data)


输出:

示例 2:

电阻

# creating a vector for city
city=c('kakumanu','hyd','noida')
  
# create a dataframe using this vector
dataframe=data.frame(city)
print(dataframe)
  
# converting city dataframe column into vector
# by passing as index
my_data = dataframe[['city']]
print("--------convert into vector of city column-------")
print(my_data)

输出: