R语言 把一个向量转为单列
在这篇文章中,我们将讨论如何在R编程语言中把一个向量从单行转换为单列。
步驟 –
- 创建一个向量
c(value1,value2,…..,valuen)
- 将该向量转换为矩阵。在R语言中,矩阵是一种二维数据结构,由行和列组成。我们可以通过使用 matrix() 函数在R中创建一个矩阵。
matrix(data)
- 查看结果数据
下面给出了使用这种方法的实现。
例子
# 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))
输出
[,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"