Open In App

Transpose a vector into single column in R

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

Steps –



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

matrix(data)



Implementation using this approach is given below.

Example:




# 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:




# 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”


Article Tags :