# create new matrices with specific patterns

4 messages
Open this post in threaded view
|

## create new matrices with specific patterns

 Dear R users, Suppose I have a matrix A. > p <- 1:4 > q <- 1:5 > P<-rep(p, each=5) > Q<-rep(q, 4) > > A <- cbind(P,Q) > A       P Q  [1,] 1 1  [2,] 1 2  [3,] 1 3  [4,] 1 4  [5,] 1 5  [6,] 2 1  [7,] 2 2  [8,] 2 3  [9,] 2 4 [10,] 2 5 [11,] 3 1 [12,] 3 2 [13,] 3 3 [14,] 3 4 [15,] 3 5 [16,] 4 1 [17,] 4 2 [18,] 4 3 [19,] 4 4 [20,] 4 5 > With the matrix A, I'd like to generate new matrices B, ..., E below. B = A[(3,4), (1,2)] C = A[(2,2), (1,5), (1,1)] D = A[(4,2)] E = A[(3,0), (1,4)] Matrix B means that first three 'p's (1,2,3) has four 'q's (1,2,3,4) and the forth 'p' element (4) has two 'q's (1,2); in other words, Is there the easyiest way to create B,...,E in R? Actually, the example above is a toy example and the matrix A I have is around 10,000 by 10,000 and the pattern is also very complicated. Any suggestion will be greatly appreciated. Best, Kathryn Lord         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: create new matrices with specific patterns

 Sorry for inconvenience. For clarification, The matrix B looks like > B       P Q  [1,] 1 1  [2,] 1 2  [3,] 1 3  [4,] 1 4  [5,] 2 1  [6,] 2 2  [7,] 2 3  [8,] 2 4 [9,] 3 1 [10,] 3 2 [11,] 3 3 [12,] 3 4 [13,] 4 1 [14,] 4 2 The matrix E is > E       P Q [1,] 4 1 [2,] 4 2 [3,] 4 3 [4,] 4 4 On Tue, Jan 27, 2015 at 11:47 AM, Kathryn Lord <[hidden email]> wrote: > Dear R users, > > Suppose I have a matrix A. > > > p <- 1:4 > > q <- 1:5 > > P<-rep(p, each=5) > > Q<-rep(q, 4) > > > > A <- cbind(P,Q) > > A >       P Q >  [1,] 1 1 >  [2,] 1 2 >  [3,] 1 3 >  [4,] 1 4 >  [5,] 1 5 >  [6,] 2 1 >  [7,] 2 2 >  [8,] 2 3 >  [9,] 2 4 > [10,] 2 5 > [11,] 3 1 > [12,] 3 2 > [13,] 3 3 > [14,] 3 4 > [15,] 3 5 > [16,] 4 1 > [17,] 4 2 > [18,] 4 3 > [19,] 4 4 > [20,] 4 5 > > > > > With the matrix A, I'd like to generate new matrices B, ..., E below. > > B = A[(3,4), (1,2)] > C = A[(2,2), (1,5), (1,1)] > D = A[(4,2)] > E = A[(3,0), (1,4)] > > > Matrix B means that first three 'p's (1,2,3) has four 'q's (1,2,3,4) and > the forth 'p' element (4) has two 'q's (1,2); in other words, > > Is there the easyiest way to create B,...,E in R? > > Actually, the example above is a toy example and the matrix A I have is > around 10,000 by 10,000 and the pattern is also very complicated. > > Any suggestion will be greatly appreciated. > > Best, > > Kathryn Lord > >         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: create new matrices with specific patterns

 CONTENTS DELETED The author has deleted this message.
Open this post in threaded view
|

## Re: create new matrices with specific patterns

 In reply to this post by Kathie Hi Kathryn, Selecting the elements of matrix A as you describe is not too difficult: select_A<-function(A,B) {  thisA<-A[A[,1] == B\$p[1],]  newmat<-matrix(c(rep(p[1],B\$nq[1]),rep(thisA[,2],length.out=B\$nq[1])),ncol=2)  for(i in 2:dim(B)[1]) {   thisA<-A[A[,1] == B\$p[i],]   newmat<-    rbind(newmat,     matrix(c(rep(B\$p[i],B\$nq[i]),rep(thisA[,2],length.out=B\$nq[i])),ncol=2))  }  return(newmat) } B<-data.frame(p=c(1,2,3,4),nq=c(4,4,4,2)) select_A(A,B) E<-data.frame(p=c(1,2,3,4),nq=c(0,0,0,4)) select_A(A,E) However, I suspect that you want to use the notation in your initial post rather than specify data frames as above. If this is the case, could you let us know how the selection of elements is to be specified and perhaps there will be a solution. Jim On Tue, Jan 27, 2015 at 2:43 PM, Kathryn Lord <[hidden email]> wrote: > Sorry for inconvenience. For clarification, > > The matrix B looks like > >> B >       P Q >  [1,] 1 1 >  [2,] 1 2 >  [3,] 1 3 >  [4,] 1 4 >  [5,] 2 1 >  [6,] 2 2 >  [7,] 2 3 >  [8,] 2 4 > [9,] 3 1 > [10,] 3 2 > [11,] 3 3 > [12,] 3 4 > [13,] 4 1 > [14,] 4 2 > > The matrix E is > >> E >       P Q > [1,] 4 1 > [2,] 4 2 > [3,] 4 3 > [4,] 4 4 > > > > On Tue, Jan 27, 2015 at 11:47 AM, Kathryn Lord <[hidden email]> > wrote: > >> Dear R users, >> >> Suppose I have a matrix A. >> >> > p <- 1:4 >> > q <- 1:5 >> > P<-rep(p, each=5) >> > Q<-rep(q, 4) >> > >> > A <- cbind(P,Q) >> > A >>       P Q >>  [1,] 1 1 >>  [2,] 1 2 >>  [3,] 1 3 >>  [4,] 1 4 >>  [5,] 1 5 >>  [6,] 2 1 >>  [7,] 2 2 >>  [8,] 2 3 >>  [9,] 2 4 >> [10,] 2 5 >> [11,] 3 1 >> [12,] 3 2 >> [13,] 3 3 >> [14,] 3 4 >> [15,] 3 5 >> [16,] 4 1 >> [17,] 4 2 >> [18,] 4 3 >> [19,] 4 4 >> [20,] 4 5 >> > >> >> >> With the matrix A, I'd like to generate new matrices B, ..., E below. >> >> B = A[(3,4), (1,2)] >> C = A[(2,2), (1,5), (1,1)] >> D = A[(4,2)] >> E = A[(3,0), (1,4)] >> >> >> Matrix B means that first three 'p's (1,2,3) has four 'q's (1,2,3,4) and >> the forth 'p' element (4) has two 'q's (1,2); in other words, >> >> Is there the easyiest way to create B,...,E in R? >> >> Actually, the example above is a toy example and the matrix A I have is >> around 10,000 by 10,000 and the pattern is also very complicated. >> >> Any suggestion will be greatly appreciated. >> >> Best, >> >> Kathryn Lord >> >> > >         [[alternative HTML version deleted]] > > ______________________________________________ > [hidden email] mailing list -- To UNSUBSCRIBE and more, see > 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.