Don't give up on for loops entirely... some of the largest time savings in optimizing loops are achieved by managing memory effectively. [1]
[1] https://www.r-bloggers.com/r-tip-use-vectormode-list-to-pre-allocate-lists On November 8, 2018 8:05:39 PM PST, li li <hannah....@gmail.com> wrote: >Hi all, > I am trying to list all the 4 by 2 tables with some fixed margins. > For example, consider 4 by 2 tables with row margins 1,2,2,1 and >column margins 3,3. I was able to do it using the code below. However, >as seen below, I had to first count the total number of tables with >the specific row margins and column margins in order to create space >to store the tables. >Is there a way to skip the step of counting the number of tables? > Also, wanted to avoid for loops as much as possible since it can be >extremely slow and inefficient. > Thanks so much in advance for you insight and help. > Hanna > > > >> library(gtools) >> A <- permutations(n=4,r=2,v=0:3, repeats.allowed=TRUE) >> B <- apply(A, 1, sum) >> rmg <- c(1,2,2,1) >> cmg <- c(3,3) >> m1 <- t(A[which(B==1),]) >> m2 <- t(A[which(B==2),]) >> m3 <- t(A[which(B==2),]) >> >> ##count number of tables with row margins 1,2,2,1 and column margins >3,3. >> num <- 0 >> for (i in 1:ncol(m1)){ >+ for (j in 1:ncol(m2)){ >+ for (k in 1:ncol(m3)){ >+ M <- t(cbind(m1[,i], m2[,j], m3[,k])) >+ M1 <- rbind(M, cmg-apply(M,2,sum)) >+ num <- num+(sum(M1[4,] < 0) == 0) >+ }}} >> >> >> #create space to store the tables >> C <- array(NA, dim=c(4,2,num)) >> >> # list all the tables with fixed margins >> num <- 0 >> for (i in 1:ncol(m1)){ >+ for (j in 1:ncol(m2)){ >+ for (k in 1:ncol(m3)){ >+ M <- t(cbind(m1[,i], m2[,j], m3[,k])) >+ M1 <- rbind(M,cmg-apply(M,2,sum)) >+ if (sum(M1[4,] < 0) == 0) { >+ num <- num+1 >+ C[,,num] <- M1 >+ } >+ }}} >> >> C >, , 1 > > [,1] [,2] >[1,] 0 1 >[2,] 0 2 >[3,] 2 0 >[4,] 1 0 > >, , 2 > > [,1] [,2] >[1,] 0 1 >[2,] 1 1 >[3,] 1 1 >[4,] 1 0 > >, , 3 > > [,1] [,2] >[1,] 0 1 >[2,] 1 1 >[3,] 2 0 >[4,] 0 1 > >, , 4 > > [,1] [,2] >[1,] 0 1 >[2,] 2 0 >[3,] 0 2 >[4,] 1 0 > >, , 5 > > [,1] [,2] >[1,] 0 1 >[2,] 2 0 >[3,] 1 1 >[4,] 0 1 > >, , 6 > > [,1] [,2] >[1,] 1 0 >[2,] 0 2 >[3,] 1 1 >[4,] 1 0 > >, , 7 > > [,1] [,2] >[1,] 1 0 >[2,] 0 2 >[3,] 2 0 >[4,] 0 1 > >, , 8 > > [,1] [,2] >[1,] 1 0 >[2,] 1 1 >[3,] 0 2 >[4,] 1 0 > >, , 9 > > [,1] [,2] >[1,] 1 0 >[2,] 1 1 >[3,] 1 1 >[4,] 0 1 > >, , 10 > > [,1] [,2] >[1,] 1 0 >[2,] 2 0 >[3,] 0 2 >[4,] 0 1 > >______________________________________________ >R-help@r-project.org 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. -- Sent from my phone. Please excuse my brevity. ______________________________________________ R-help@r-project.org 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.