将向量转置为 R 中的单列
在本文中,我们将讨论如何在 R 编程语言中将向量从单行转换为列。
脚步 -
- 创建一个向量
c(value1,value2,…..,valuen)
- 将向量转换为矩阵。在 R 中,矩阵是由行和列组成的二维数据结构。我们可以使用matrix()函数在 R 中创建一个矩阵。
matrix(data)
- 查看结果数据
下面给出使用这种方法的实现。
例子:
R
# vector 1 is created with nested vector
data1=c("sravan","bobby","pinkey","rohith",
"gnanesh",'divya',"satwik")
print(data1)
# pass data1 vector into a matrix
print(matrix(data1))
R
# vector 1 is created with nested vector
data1=c("sravan","bobby","pinkey","rohith",
"gnanesh",'divya',"satwik",
c(1,2,3,4,5))
print(data1)
# pass data1 vector into a matrix
print(matrix(data1))
输出:
[,1]
[1,] “sravan”
[2,] “bobby”
[3,] “pinkey”
[4,] “rohith”
[5,] “gnanesh”
[6,] “divya”
[7,] “satwik”
一个向量也可以嵌套到另一个向量中,但因为它是同一个向量的一部分,所以它会出现在同一个向量中。
例子:
电阻
# vector 1 is created with nested vector
data1=c("sravan","bobby","pinkey","rohith",
"gnanesh",'divya',"satwik",
c(1,2,3,4,5))
print(data1)
# pass data1 vector into a matrix
print(matrix(data1))
输出:
[,1]
[1,] “sravan”
[2,] “bobby”
[3,] “pinkey”
[4,] “rohith”
[5,] “gnanesh”
[6,] “divya”
[7,] “satwik”
[8,] “1”
[9,] “2”
[10,] “3”
[11,] “4”
[12,] “5”