Open In App
Related Articles

Transpose a vector into single column in R

Improve Article
Improve
Save Article
Save
Like Article
Like

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”


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 05 Apr, 2021
Like Article
Save Article
Similar Reads
Related Tutorials