📜  从 R 中的数据帧中提取向量

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

从 R 中的数据帧中提取向量

在本文中,我们将看到如何在 R 编程语言中从 DataFrame 中提取向量。

方法很简单,通过使用$ 运算符 ,我们可以将数据帧列转换为向量。

句法:

下面给出了实现相同功能的各种示例



示例 1:

R
# create vector with names
name=c("sravan","mohan","sudheer","radha","vani","mohan")
  
# create vector with subjects
subjects=c(".net","Python","java","dbms","os","dbms")
  
# create a vector with marks
marks=c(98,97,89,90,87,90)
  
# create vector with height
height=c(5.97,6.11,5.89,5.45,5.78,6.0)
  
# create vector with weight
weight=c(67,65,78,65,81,76)
  
# pass these vectors to the data frame
data=data.frame(name,subjects,marks,height,weight)
  
# display dataframe
print(data)
  
# access vector from dataframe column name
a=(data$name)
print(a)
  
# access vector from dataframe column marks
b=(data$marks)
print(b)


R
# create vector with height
height=c(5.97,6.11,5.89,5.45,5.78,6.0)
  
# create vector with weight
weight=c(67,65,78,65,81,76)
  
# pass these vectors to the data frame
data=data.frame(height,weight)
  
# display dataframe
print(data)
  
# access vector from dataframe column
# weight
a=(data$weight)
print(a)
  
# access vector from dataframe column 
# height
b=(data$height)
print(b)


输出:

示例 2:

电阻

# create vector with height
height=c(5.97,6.11,5.89,5.45,5.78,6.0)
  
# create vector with weight
weight=c(67,65,78,65,81,76)
  
# pass these vectors to the data frame
data=data.frame(height,weight)
  
# display dataframe
print(data)
  
# access vector from dataframe column
# weight
a=(data$weight)
print(a)
  
# access vector from dataframe column 
# height
b=(data$height)
print(b)

输出: