Could someone please suggest a more clever solution to the following problem than my loop below? Given X a 2xN matrix X, and I a k-subset of N, Generate the (2^k)xN matrix Y with columns not in I all zero and the other columns with all choices of an entry from the first or second row of X. For example, with X <- matrix(1:8, nrow=2) I <- c(1,3) X is 1 3 5 7 2 4 6 8 and Y should be 1 0 5 0 2 0 5 0 1 0 6 0 2 0 6 0 The order of the rows is unimportant. --- I solved this using a loop over the rows of Y after forming some preliminary matrices. I think it could be improved. N <- NCOL(X) k <- length(I) G <- as.matrix(expand.grid(rep(list(c(1,2)),k))) Y <- matrix(0,nc=N,nr=NROW(G)) for(i in 1:NROW(G)){ ind <- rep(1,N) ind[I] <- G[i,] Y[i,] <- X[array(c(ind,1:N),dim=c(N,2))] } Y[,-I] <- 0 David L. Reiner ?? Rho Trading 440 S. LaSalle St -- Suite 620 Chicago?? IL?? 60605 ?? 312-362-4963 (voice) 312-362-4941 (fax) ??
If I understand you correctly, here's one way based on expand.grid(). I is just an index set, and so all you really need to do is generate your 2^k rows from the part of the matrix you're using in the right places via replacement: e.g. newX<-matrix(0, ncol=ncol(X),nrow=2^length(I)) newX[,I]<-expand.grid(as.list(as.data.frame(X[,I]))) N.B. I tried to do the this without the explicit as.list() cast, but got an error message. I would have thought that expand.grid should have recognized that a data.frame IS a list without the cast. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > davidr at rhotrading.com > Sent: Friday, May 06, 2005 12:06 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Choices from a matrix > > Could someone please suggest a more clever solution to the > following problem than my loop below? > > Given X a 2xN matrix X, and I a k-subset of N, > Generate the (2^k)xN matrix Y with columns not in I all zero > and the other columns with all choices of an entry from the > first or second row of X. > > For example, with > X <- matrix(1:8, nrow=2) > I <- c(1,3) > > X is > 1 3 5 7 > 2 4 6 8 > > and Y should be > 1 0 5 0 > 2 0 5 0 > 1 0 6 0 > 2 0 6 0 > > The order of the rows is unimportant. > --- > I solved this using a loop over the rows of Y after forming > some preliminary matrices. I think it could be improved. > > N <- NCOL(X) > k <- length(I) > G <- as.matrix(expand.grid(rep(list(c(1,2)),k))) > Y <- matrix(0,nc=N,nr=NROW(G)) > > for(i in 1:NROW(G)){ > ind <- rep(1,N) > ind[I] <- G[i,] > Y[i,] <- X[array(c(ind,1:N),dim=c(N,2))] > } > Y[,-I] <- 0 > > > > > David L. Reiner > ?? > Rho Trading > 440 S. LaSalle St -- Suite 620 > Chicago?? IL?? 60605 > ?? > 312-362-4963 (voice) > 312-362-4941 (fax) > ?? > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Thanks, Bert, but when I tried that, I didn't get a matrix:> newX[[1]] [1] 1 2 1 2 [[2]] [1] 5 5 6 6 [[3]] [1] 1 2 1 2 [[4]] [1] 5 5 6 6 [[5]] [1] 0 [[6]] [1] 0 [[7]] [1] 0 [[8]] [1] 0 [[9]] [1] 1 2 1 2 [[10]] [1] 5 5 6 6 [[11]] [1] 1 2 1 2 [[12]] [1] 5 5 6 6 [[13]] [1] 0 [[14]] [1] 0 [[15]] [1] 0 [[16]] [1] 0 The matrix is in there, four times in fact. For larger problems, I believe it would be in 2^k times? It is certainly a one-liner, though, so if the matrix could be extracted simply, it could be of use. Best regards, David -----Original Message----- From: Berton Gunter [mailto:gunter.berton at gene.com] Sent: Friday, May 06, 2005 2:49 PM To: David Reiner <davidr at rhotrading.com>; r-help at stat.math.ethz.ch Subject: RE: [R] Choices from a matrix If I understand you correctly, here's one way based on expand.grid(). I is just an index set, and so all you really need to do is generate your 2^k rows from the part of the matrix you're using in the right places via replacement: e.g. newX<-matrix(0, ncol=ncol(X),nrow=2^length(I)) newX[,I]<-expand.grid(as.list(as.data.frame(X[,I]))) N.B. I tried to do the this without the explicit as.list() cast, but got an error message. I would have thought that expand.grid should have recognized that a data.frame IS a list without the cast. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > davidr at rhotrading.com > Sent: Friday, May 06, 2005 12:06 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Choices from a matrix > > Could someone please suggest a more clever solution to the > following problem than my loop below? > > Given X a 2xN matrix X, and I a k-subset of N, > Generate the (2^k)xN matrix Y with columns not in I all zero > and the other columns with all choices of an entry from the > first or second row of X. > > For example, with > X <- matrix(1:8, nrow=2) > I <- c(1,3) > > X is > 1 3 5 7 > 2 4 6 8 > > and Y should be > 1 0 5 0 > 2 0 5 0 > 1 0 6 0 > 2 0 6 0 > > The order of the rows is unimportant. > --- > I solved this using a loop over the rows of Y after forming > some preliminary matrices. I think it could be improved. > > N <- NCOL(X) > k <- length(I) > G <- as.matrix(expand.grid(rep(list(c(1,2)),k))) > Y <- matrix(0,nc=N,nr=NROW(G)) > > for(i in 1:NROW(G)){ > ind <- rep(1,N) > ind[I] <- G[i,] > Y[i,] <- X[array(c(ind,1:N),dim=c(N,2))] > } > Y[,-I] <- 0 > > > > > David L. Reiner > ?? > Rho Trading > 440 S. LaSalle St -- Suite 620 > Chicago?? IL?? 60605 > ?? > 312-362-4963 (voice) > 312-362-4941 (fax) > ?? > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Bert, that was almost it, we just need one more as.matrix: newX[,I]<-as.matrix(expand.grid(as.list(as.data.frame(X[,I])))) This works as advertised. Best regards, David p.s. I agree that the extra as.list is counterintuitive. It might be nice if there were a matrix form of expand.grid. -----Original Message----- From: Berton Gunter [mailto:gunter.berton at gene.com] Sent: Friday, May 06, 2005 2:49 PM To: David Reiner <davidr at rhotrading.com>; r-help at stat.math.ethz.ch Subject: RE: [R] Choices from a matrix If I understand you correctly, here's one way based on expand.grid(). I is just an index set, and so all you really need to do is generate your 2^k rows from the part of the matrix you're using in the right places via replacement: e.g. newX<-matrix(0, ncol=ncol(X),nrow=2^length(I)) newX[,I]<-expand.grid(as.list(as.data.frame(X[,I]))) N.B. I tried to do the this without the explicit as.list() cast, but got an error message. I would have thought that expand.grid should have recognized that a data.frame IS a list without the cast. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > davidr at rhotrading.com > Sent: Friday, May 06, 2005 12:06 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Choices from a matrix > > Could someone please suggest a more clever solution to the > following problem than my loop below? > > Given X a 2xN matrix X, and I a k-subset of N, > Generate the (2^k)xN matrix Y with columns not in I all zero > and the other columns with all choices of an entry from the > first or second row of X. > > For example, with > X <- matrix(1:8, nrow=2) > I <- c(1,3) > > X is > 1 3 5 7 > 2 4 6 8 > > and Y should be > 1 0 5 0 > 2 0 5 0 > 1 0 6 0 > 2 0 6 0 > > The order of the rows is unimportant. > --- > I solved this using a loop over the rows of Y after forming > some preliminary matrices. I think it could be improved. > > N <- NCOL(X) > k <- length(I) > G <- as.matrix(expand.grid(rep(list(c(1,2)),k))) > Y <- matrix(0,nc=N,nr=NROW(G)) > > for(i in 1:NROW(G)){ > ind <- rep(1,N) > ind[I] <- G[i,] > Y[i,] <- X[array(c(ind,1:N),dim=c(N,2))] > } > Y[,-I] <- 0 > > > > > David L. Reiner > ?? > Rho Trading > 440 S. LaSalle St -- Suite 620 > Chicago?? IL?? 60605 > ?? > 312-362-4963 (voice) > 312-362-4941 (fax) > ?? > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Quite right (ALWAYS TEST, Bert!), because expand.grid() returns a data.frame. But all you have to do is create newX as a data.frame: newX<-data.frame(matrix(0, ncol=ncol(X),nrow=2^length(I))) and **now** it will work:>X<-matrix(1:6,nr=2) >I<- c(1,3) >newX<-data.frame(matrix(0, ncol=ncol(X),nrow=2^length(I))) >newX[,I]<-expand.grid(as.list(as.data.frame(X[,I]))) >newXX1 X2 X3 1 1 0 5 2 2 0 5 3 1 0 6 4 2 0 6 Note that newX is a data.frame -- you may wish to recast it as a matrix. Of course, I'm still not sure that this is what you wanted. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: bgunter > Sent: Friday, May 06, 2005 12:49 PM > To: davidr at rhotrading.com; r-help at stat.math.ethz.ch > Subject: RE: [R] Choices from a matrix > > If I understand you correctly, here's one way based on expand.grid(). > > I is just an index set, and so all you really need to do is > generate your 2^k rows from the part of the matrix you're > using in the right places via replacement: > > e.g. newX<-matrix(0, ncol=ncol(X),nrow=2^length(I)) > newX[,I]<-expand.grid(as.list(as.data.frame(X[,I]))) > > > N.B. I tried to do the this without the explicit as.list() > cast, but got an error message. I would have thought that > expand.grid should have recognized that a data.frame IS a > list without the cast. > > -- Bert Gunter > Genentech Non-Clinical Statistics > South San Francisco, CA > > "The business of the statistician is to catalyze the > scientific learning process." - George E. P. Box > > > > > -----Original Message----- > > From: r-help-bounces at stat.math.ethz.ch > > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > > davidr at rhotrading.com > > Sent: Friday, May 06, 2005 12:06 PM > > To: r-help at stat.math.ethz.ch > > Subject: [R] Choices from a matrix > > > > Could someone please suggest a more clever solution to the > > following problem than my loop below? > > > > Given X a 2xN matrix X, and I a k-subset of N, > > Generate the (2^k)xN matrix Y with columns not in I all zero > > and the other columns with all choices of an entry from the > > first or second row of X. > > > > For example, with > > X <- matrix(1:8, nrow=2) > > I <- c(1,3) > > > > X is > > 1 3 5 7 > > 2 4 6 8 > > > > and Y should be > > 1 0 5 0 > > 2 0 5 0 > > 1 0 6 0 > > 2 0 6 0 > > > > The order of the rows is unimportant. > > --- > > I solved this using a loop over the rows of Y after forming > > some preliminary matrices. I think it could be improved. > > > > N <- NCOL(X) > > k <- length(I) > > G <- as.matrix(expand.grid(rep(list(c(1,2)),k))) > > Y <- matrix(0,nc=N,nr=NROW(G)) > > > > for(i in 1:NROW(G)){ > > ind <- rep(1,N) > > ind[I] <- G[i,] > > Y[i,] <- X[array(c(ind,1:N),dim=c(N,2))] > > } > > Y[,-I] <- 0 > > > > > > > > > > David L. Reiner > > ?? > > Rho Trading > > 440 S. LaSalle St -- Suite 620 > > Chicago?? IL?? 60605 > > ?? > > 312-362-4963 (voice) > > 312-362-4941 (fax) > > ?? > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide! > > http://www.R-project.org/posting-guide.html > > >
Maybe Matching Threads
- COM dates (was origin and "origin<-" in chron)
- Can I find the datetime an object was last assigned to/saved?
- standard errors for orthogonal linear regression
- How to print landscape from script in Windows: dev.print(win.print, printer="local printer name", ...) does not accept horizontal=TRUE
- R 2.1.0 for Windows installation error? atanh not in R.dll?