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..!!!! any help would appreciate -- View this message in context: http://r.789695.n4.nabble.com/Create-Matrix-of-2-Dim-from-two-vectors-tp3031718p3031718.html Sent from the R help mailing list archive at Nabble.com.
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 On Mon, Nov 8, 2010 at 11:15 AM, abotaha <yaseen0202 at gmail.com> wrote:> > 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..!!!! > any help would appreciate > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Create-Matrix-of-2-Dim-from-two-vectors-tp3031718p3031718.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Hi, First, create a MATRIX of the correct size: xy <- matrix(nrow=3, ncol=3) Then, in your for loop, you need to index each cell correctly, like this: xy[i,j] Finally, if you want to assign "1,4" to each cell, you need to paste x[i] and y[j] together, like this: xy[i,j]<-paste(x[i],y[j], sep=",") With the brackets if you want: xy[i,j]<-paste("(", x[i], ",", y[j], ")", sep="") Or easier like this: xy <- matrix(rep(x,3),nrow=3, ncol=3) apply(xy, 1, FUN=paste, y, sep=",") HTH, Ivan Le 11/8/2010 11:15, abotaha a ?crit :> 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..!!!! > any help would appreciate > > > >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
Hi: If you want the literal character strings, this works: x<-c(1, 2, 3)> y<-c(4,5,6) > outer(x, y, function(x, y) paste('(', x, ',', y, ')', sep = '') )[,1] [,2] [,3] [1,] "(1,4)" "(1,5)" "(1,6)" [2,] "(2,4)" "(2,5)" "(2,6)" [3,] "(3,4)" "(3,5)" "(3,6)" However, I have the sense you want to use the values of x and y as coordinates in a function. This is also a job for outer(); e.g., f <- function(x, y) x + 0.5 * y outer(x, y, f) [,1] [,2] [,3] [1,] 3 3.5 4 [2,] 4 4.5 5 [3,] 5 5.5 6 If you want to apply a two-dimensional function to a pair of vectors, outer() may be what you're after. HTH, Dennis On Mon, Nov 8, 2010 at 2:15 AM, abotaha <yaseen0202@gmail.com> wrote:> > 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..!!!! > any help would appreciate > > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Create-Matrix-of-2-Dim-from-two-vectors-tp3031718p3031718.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Thanks a lot guys...all of your solution are useful, and good. once again thanks. -- View this message in context: http://r.789695.n4.nabble.com/Create-Matrix-of-2-Dim-from-two-vectors-tp3031718p3031797.html Sent from the R help mailing list archive at Nabble.com.