Skip to content
Related Articles

Related Articles

Improve Article

Transpose a vector into single column in R

  • Last Updated : 05 Apr, 2021

In this article, we will discuss how to convert a vector from single row to a column in R Programming Language.

Steps –

  • Create a vector

c(value1,value2,…..,valuen)

  • Convert the vector into the matrix. In R, Matrix is a two-dimensional data structure that comprises rows and columns. We can create a matrix in R, by using matrix() function.

matrix(data)

  • View the resultant data

Implementation using this approach is given below.



Example:

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))

Output:

     [,1]      

[1,] “sravan”  

[2,] “bobby”  

[3,] “pinkey”  

[4,] “rohith”  



[5,] “gnanesh”

[6,] “divya”  

[7,] “satwik” 

A vector can also be nested into another but since it is a part of the same vector it will appear in the same 

Example:

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))

Output:

      [,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”




My Personal Notes arrow_drop_up
Recommended Articles
Page :