Stock Beaver
2013-Oct-15 23:11 UTC
[R] A 'good' way to build a matrix from a sequence of integers?
# I understand that a good way to build a vector from a sequence of integers, # is to use syntax like this: myvec = c(1:99) # Here is the 'short' version of my question: # I want to understand a 'good' way to build a matrix from a sequence of integers. # If that question is not clear, here is a longer version: # Here is what I did for a 1D-matrix: # I pick the sequence 1:3 # I build a vector: vec1x3 = c(1:3) vec1x3 # I transform it into a 1 x 3 matrix: m1x3 = matrix(vec1x3, c(length(vec1x3),1)) m1x3 # [,1] # [1,] 1 # [2,] 2 # [3,] 3 # > # That was easy. # Next I want to expand from a 1 x 3 matrix to a 2 x 9 matrix # which contains all combinations of 1:3 # So the first 4 rows would look like this: # 1 1 # 1 2 # 1 3 I call this a rowvec # 2 1 # My first idea is write a loop like this: for (i in 1:3) { for(j in 1:3) { rowvec = c(i,j) # Place rowvec in matrix } } # I'm curious if a skilled R-person would do it differently? [[alternative HTML version deleted]]
Kevin E. Thorpe
2013-Oct-15 23:18 UTC
[R] A 'good' way to build a matrix from a sequence of integers?
On 10/15/2013 07:11 PM, Stock Beaver wrote:> # I understand that a good way to build a vector from a sequence of integers, > # is to use syntax like this: > myvec = c(1:99)First, the c() is not needed here. myvec <- 1:99 works just fine.> > # Here is the 'short' version of my question: > # I want to understand a 'good' way to build a matrix from a sequence of integers. > > # If that question is not clear, here is a longer version: > > # Here is what I did for a 1D-matrix: > > # I pick the sequence 1:3 > # I build a vector: > vec1x3 = c(1:3) > vec1x3 > # I transform it into a 1 x 3 matrix: > m1x3 = matrix(vec1x3, c(length(vec1x3),1)) > m1x3 > # [,1] > # [1,] 1 > # [2,] 2 > # [3,] 3 > # > > > # That was easy. > > # Next I want to expand from a 1 x 3 matrix to a 2 x 9 matrix > # which contains all combinations of 1:3I think you want expand.grid. expand.grid(x1=1:3, x2=1:3)> > # So the first 4 rows would look like this: > # 1 1 > # 1 2 > # 1 3 I call this a rowvec > # 2 1 > > # My first idea is write a loop like this: > > for (i in 1:3) { > for(j in 1:3) { > rowvec = c(i,j) > # Place rowvec in matrix > } > } > > # I'm curious if a skilled R-person would do it differently? > [[alternative HTML version deleted]] > > > > ______________________________________________ > 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. >-- Kevin E. Thorpe Head of Biostatistics, Applied Health Research Centre (AHRC) Li Ka Shing Knowledge Institute of St. Michael's Assistant Professor, Dalla Lana School of Public Health University of Toronto email: kevin.thorpe at utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016
arun
2013-Oct-15 23:26 UTC
[R] A 'good' way to build a matrix from a sequence of integers?
Hi, You could use: ?as.matrix(expand.grid(vec1x3,vec1x3)) #or as.matrix(expand.grid(rep(list(vec1x3),2))) #or library(gtools) permutations(3, 2, vec1x3, repeats.allowed=TRUE) A.K. On Tuesday, October 15, 2013 7:14 PM, Stock Beaver <stockbeaver at ymail.com> wrote: # I understand that a good way to build a vector from a sequence of integers, # is to use syntax like this: myvec = c(1:99) # Here is the 'short' version of my question: # I want to understand a 'good' way to build a matrix from a sequence of integers. # If that question is not clear, here is a longer version: # Here is what I did for a 1D-matrix: # I pick the sequence 1:3 # I build a vector: vec1x3 = c(1:3) vec1x3 # I transform it into a 1 x 3 matrix: m1x3 = matrix(vec1x3, c(length(vec1x3),1)) m1x3 #????? [,1] # [1,]??? 1 # [2,]??? 2 # [3,]??? 3 # > # That was easy. # Next I want to expand from a 1 x 3 matrix to a 2 x 9 matrix # which contains all combinations of 1:3 # So the first 4 rows would look like this: # 1 1 # 1 2 # 1 3 I call this a rowvec # 2 1 # My first idea is write a loop like this: for (i in 1:3) { ? for(j in 1:3) { ??? rowvec = c(i,j) ??? # Place rowvec in matrix ? } } # I'm curious if a skilled R-person would do it differently? ??? [[alternative HTML version deleted]] ______________________________________________ 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.