将数据帧行和列转换为 R 中的向量
在本文中,我们将在 R 编程语言中将数据帧列转换为向量,将数据帧行转换为向量。
将数据框列转换为向量
我们在数据框中获取一列,并使用选择方法将其传递给另一个变量。选择方法可以定义为使用“[[]]”运算符从数据框中选择一列。
脚步 -
- 创建数据框
- 选择要转换的列
- 将其分配给变量
- 显示如此生成的数据框。
句法:
dataframe[[‘column’]]
例子:
R
# create vectors
id=c(7058,7084,7098)
name=c('sravan','karthik','nikhil')
# passing into dataframe
data=data.frame(id,name)
print(data)
# convert id column into a vector
column_data=data[['id']]
print(column_data)
# convert name column into a vector
column_data1=data[['name']]
print(column_data1)
R
# create vectors
id=c(7058,7084,7098)
name=c('sravan','karthik','nikhil')
# passing into dataframe
data=data.frame(id,name)
print(data)
print("-----------")
# converting 1 st row to a vector
as.vector((data[1,]))
print("-----------")
# converting 2nd row to a vector
as.vector((data[2,]))
print("-----------")
# converting 3 rd row to a vector
as.vector((data[3,]))
print("-----------")
R
# create vectors
id=c(7058,7084,7098)
name=c('sravan','karthik','nikhil')
# passing into dataframe
data=data.frame(id,name)
print(data)
print("-----------")
# converting 1 st row to a vector
as.vector(t(data[1,]))
print("-----------")
# converting 2nd row to a vector
as.vector(t(data[2,]))
print("-----------")
# converting 3 rd row to a vector
as.vector(t(data[3,]))
print("-----------")
R
# create vectors
id=c(7058,7084,7098)
name=c('sravan','karthik','nikhil')
# passing into dataframe
data=data.frame(id,name)
print(data)
print("-----------")
# converting all dataframe to a vector
as.vector(t(data))
R
# create vectors
id=c(7058,7084,7098)
address=c('guntur','hyd','kothapeta')
name=c('sravan','karthik','nikhil')
# passing into dataframe
data=data.frame(id,address,name)
print(data)
print("-----dataframe row to a vector------")
# converting dataframe to a vector
as.vector(t(data))
print("-----dataframe column to a vector------")
# converting dataframe 1 st column to a vector
data1=data[['id']]
print(data1)
# converting dataframe 2 nd column to a vector
data1=data[['address']]
print(data1)
# converting dataframe 3 rd column to a vector
data1=data[['name']]
print(data1)
输出:
将数据框行转换为向量
我们可以使用称为 as.vector() 的方法转换每一行或整个数据帧
方法
- 创建数据框
- 选择要转换的行
- 传递给函数
- 显示结果
句法:
as.vector(t(dataframe_name))
其中 t 是数据帧的转置。如果未指定 t,则输出为行名和列名。如果指定输出只是行。
示例:没有 t 规格。
电阻
# create vectors
id=c(7058,7084,7098)
name=c('sravan','karthik','nikhil')
# passing into dataframe
data=data.frame(id,name)
print(data)
print("-----------")
# converting 1 st row to a vector
as.vector((data[1,]))
print("-----------")
# converting 2nd row to a vector
as.vector((data[2,]))
print("-----------")
# converting 3 rd row to a vector
as.vector((data[3,]))
print("-----------")
输出:
示例:使用 t
电阻
# create vectors
id=c(7058,7084,7098)
name=c('sravan','karthik','nikhil')
# passing into dataframe
data=data.frame(id,name)
print(data)
print("-----------")
# converting 1 st row to a vector
as.vector(t(data[1,]))
print("-----------")
# converting 2nd row to a vector
as.vector(t(data[2,]))
print("-----------")
# converting 3 rd row to a vector
as.vector(t(data[3,]))
print("-----------")
输出:
将整个数据框显示为向量
例子:
电阻
# create vectors
id=c(7058,7084,7098)
name=c('sravan','karthik','nikhil')
# passing into dataframe
data=data.frame(id,name)
print(data)
print("-----------")
# converting all dataframe to a vector
as.vector(t(data))
输出:
示例 2:
电阻
# create vectors
id=c(7058,7084,7098)
address=c('guntur','hyd','kothapeta')
name=c('sravan','karthik','nikhil')
# passing into dataframe
data=data.frame(id,address,name)
print(data)
print("-----dataframe row to a vector------")
# converting dataframe to a vector
as.vector(t(data))
print("-----dataframe column to a vector------")
# converting dataframe 1 st column to a vector
data1=data[['id']]
print(data1)
# converting dataframe 2 nd column to a vector
data1=data[['address']]
print(data1)
# converting dataframe 3 rd column to a vector
data1=data[['name']]
print(data1)
输出: