Search This Blog

Tuesday, March 27, 2012

R: binding two vectors and display it as a matrix in R

Hello,

I have two data.

x<-c(1, 2, 3)
y<-c(4,5,6)

How do i create matrix of 3 by 3 from this two, such that  

 (1,4)  (1,5)  (1,6)
 (2,4)  (2,5)  (2,6)
 (3,4)  (3,5)  (3,6)

I tried some thing like this:

xy <- as.data.frame(c(0,0,0), dim=c(3,3))
for(i in 1:3)
for(j in 1:3)
xy[i][j]<-c(x[i],y[j])

but i got errors..!!!!

Try
 x1<-matrix(1,3,1)%x%x
 y1<-y%x%matrix(1,3,1)

Z<-cbind(x1,y1)
And later you need to move towards list and matrix

No comments:

Post a Comment

Thank you