rose
rose

Reputation: 1981

Create a matrix in R by changing the order of vector

I am new user of R. I have two vectors in R and I want to put thses vectors in a matrix such as follows:

    x = c(1, 2, 3, 5, 4)
    y = c(1.1, 2.3, 4.5, 6.7, 5.5)
    > m
         [,1] [,2]
   [1,]    1  1.1
   [2,]    2  2.3
   [3,]    3  4.5
   [4,]    5  5.5
   [5,]    4  6.7

How can i do this in R?

Upvotes: 1

Views: 147

Answers (2)

IRTFM
IRTFM

Reputation: 263362

If the x vector were not sequential you could still get success with:

cbind(x, y[order(x)] )

Upvotes: 2

thelatemail
thelatemail

Reputation: 93813

You can get there with:

cbind(x,y[x])


     x    
[1,] 1 1.1
[2,] 2 2.3
[3,] 3 4.5
[4,] 5 5.5
[5,] 4 6.7

Upvotes: 3

Related Questions