[R] Alternating between "for loops"

Rui Barradas ruipbarradas at sapo.pt
Wed Aug 1 01:54:36 CEST 2012


Hello,

You're right, and there's a way without loops. Summarizing:


J <- 10
N <- 10

cols <- rep(c(TRUE, TRUE, FALSE, FALSE), ceiling(J / 4))[seq_len(J)]

#--- 1st way: nested loops
y <- matrix(0, N, J)
for(j in which(cols)){
     for (q in 1:N)
         y[q, j] <- if(j %% 2) 1 else 2
}
for(j in which(!cols)){
     for (q in 1:N)
         y[q, j] <- if(j %% 2) "A" else "B"
}

#--- 2nd way: one loop only
z <- matrix(0, N, J)
for(j in which(cols))
     z[, j] <- if(j %% 2) 1 else 2
for(j in which(!cols))
     z[, j] <- if(j %% 2) "A" else "B"

#--- 3rd way: no loops
w <- matrix(0, J, N)  # reversed order
w[cols, ] <- ifelse(which(cols) %% 2, 1, 2)
w[!cols, ] <- ifelse(which(!cols) %% 2, "A", "B")
w <- t(w)

identical(y, z) # compare original to each
identical(y, w) # of the other results

Em 31-07-2012 23:54, Mercier Eloi escreveu:
> On 12-07-31 02:38 PM, Claudia Penaloza wrote:
>> Thank you very much Rui (and Eloi for your input)... this is (the very
>> simplified version of) what I will end up using:
> Could we get the extended version ? Because right now, I don't know why
> you need such complicated code that can be done in 1 line.
>> J <- 10
>> N <- 10
>> y <- matrix(0,N,J)
>> cols <- rep(c(TRUE, TRUE, FALSE, FALSE), 3)[seq_len(J)]
>> for(j in which(cols)){
>>    for (q in 1:N){
>>      if(j %% 2){
>>        y[q,j]=1
>>        }else{y[q,j]=2}
>>      }
>>    }
>> for(j in which(!cols)){
>>    for (q in 1:N){
>>      if(j %% 2){
>>        y[q,j]="A"
>>        }else{y[q,j]="B"}
>>      }
>>    }
>>
> There is no need for a double loop (on N) :
>
> J <- 10
> N <- 10
> y <- matrix(0,N,J)
> cols <- rep(c(TRUE, TRUE, FALSE, FALSE), 3)[seq_len(J)]
> for(j in which(cols)){
>      if(j %% 2){
>         y[,j]=1
>         }else{y[,j]=2}
>     }
> for(j in which(!cols)){
>       if(j %% 2){
>         y[,j]="A"
>         }else{y[,j]="B"}
>     }
>
> if you really wants to use this code.
>
> Cheers,
>
> Eloi
>> Cheers,
>> Claudia
>> On Mon, Jul 30, 2012 at 5:38 PM, Mercier Eloi <emercier at chibi.ubc.ca
>> <mailto:emercier at chibi.ubc.ca>> wrote:
>>
>>      Or, assuming you only have 4 different elements :
>>
>>      mat<- matrix(rep(c(1,2,"A", "B"),each=10),10,10, byrow=F)
>>      mat2 <- as.data.frame(mat)
>>
>>      mat
>>
>>            [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>>       [1,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>       [2,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>       [3,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>       [4,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>       [5,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>       [6,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>       [7,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>       [8,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>       [9,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>      [10,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>
>>      mat2
>>         V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
>>
>>      1   1  2  A  B  1  2  A  B  1   2
>>      2   1  2  A  B  1  2  A  B  1   2
>>      3   1  2  A  B  1  2  A  B  1   2
>>      4   1  2  A  B  1  2  A  B  1   2
>>      5   1  2  A  B  1  2  A  B  1   2
>>      6   1  2  A  B  1  2  A  B  1   2
>>      7   1  2  A  B  1  2  A  B  1   2
>>      8   1  2  A  B  1  2  A  B  1   2
>>      9   1  2  A  B  1  2  A  B  1   2
>>      10  1  2  A  B  1  2  A  B  1   2
>>
>>      Cheers,
>>
>>      Eloi
>>
>>
>>      On 12-07-30 04:28 PM, Rui Barradas wrote:
>>
>>          Hello,
>>
>>          Maybe something along the lines of
>>
>>          J <- 10
>>          cols <- rep(c(TRUE, TRUE, FALSE, FALSE), 3)[seq_len(J)]
>>          for(i in which(cols)) { do something }
>>          for(i in which(!cols)) { do something else }
>>
>>          Hope this helps,
>>
>>          Rui Barradas
>>
>>          Em 31-07-2012 00 <tel:31-07-2012%2000>:18, Claudia Penaloza
>>          escreveu:
>>
>>              Dear All,
>>              I would like to apply two different "for loops" to each
>>              set of four columns
>>              of a matrix (the loops here are simplifications of the
>>              actual loops I will
>>              be running which involve multiple if/else statements).
>>              I don't know how to "alternate" between the loops
>>              depending on which column
>>              is "running through the loop" at the time.
>>              ## Set up matrix
>>              J <- 10
>>              N <- 10
>>              y <- matrix(0,N,J)
>>              ## Apply this loop to the first two of every four columns
>>              ([,1:2],
>>              [,5:6],[,9:10], etc.)
>>              for (q in 1:N){
>>              for(j in 1:J){
>>              if(j %% 2){
>>              y[q,j]=1
>>              }else{y[q,j]=2}
>>              }
>>              }
>>              ## Apply this loop to the next two of every four columns
>>              ([,3:4],[,7:8],[,11:12], etc.)
>>              for (q in 1:N){
>>              for(j in 1:J){
>>              if(j %% 2){
>>              y[q,j]="A"
>>              }else{y[q,j]="B"}
>>              }
>>              }
>>              I want to get something like this (preferably without the
>>              quotes):
>>
>>                  y
>>
>>              [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>>                [1,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>                [2,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>                [3,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>                [4,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>                [5,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>                [6,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>                [7,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>                [8,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>                [9,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>              [10,] "1"  "2"  "A"  "B"  "1"  "2"  "A"  "B"  "1"  "2"
>>
>>
>>
>>              Any help greatly appreciated!
>>
>>              Claudia
>>
>>                  [[alternative HTML version deleted]]
>>
>>              ______________________________________________
>>              R-help at r-project.org <mailto: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.
>>
>>
>>          ______________________________________________
>>          R-help at r-project.org <mailto: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.
>>
>>
>>
>>
>>      --
>>      Eloi Mercier
>>      Bioinformatics PhD Student, UBC
>>      Paul Pavlidis Lab
>>      2185 East Mall
>>      University of British Columbia
>>      Vancouver BC V6T1Z4
>>
>>
>



More information about the R-help mailing list