Get or Set the Structure of a Vector in R Programming – structure() Function
Last Updated :
25 Jun, 2020
structure()
function in R Language is used to get or set the structure of a vector by changing its dimensions.
Syntax: structure(vec, dim)
Parameters:
vec: Vector
dim: New Dimensions
Example 1:
x1 < - c( 1 : 10 )
structure(x1)
x2 < - c( "abc" , "cde" , "def" )
structure(x2)
|
Output:
[1] 1 2 3 4 5 6 7 8 9 10
[1] "abc" "cde" "def"
Example 2:
x < - c( 1 : 10 )
x
y1 < - structure(x, dim = c( 2 , 5 ))
y2 < - structure(x, dim = c( 5 , 2 ))
y1
y2
|
Output:
[1] 1 2 3 4 5 6 7 8 9 10
[, 1] [, 2] [, 3] [, 4] [, 5]
[1, ] 1 3 5 7 9
[2, ] 2 4 6 8 10
[, 1] [, 2]
[1, ] 1 6
[2, ] 2 7
[3, ] 3 8
[4, ] 4 9
[5, ] 5 10
Share your thoughts in the comments
Please Login to comment...