[R] rbind in a loop with " : " elements

Rui Barradas ruipbarradas at sapo.pt
Wed Aug 22 20:22:30 CEST 2012


Hello,

I'm being lazy, but the following works.

r = 3 ; set.seed(1)
v <- matrix(c(rnorm(40)),10,4)

result <- vector("list", r*ncol(v))
ires <- 0

for (j in 1:4){
     for (i in 1:r){
         x <- t(v[,j]^(i)*v[,1:4]^((r-(4-1)):r))
         #print(x)
         ires <- ires + 1
         result[[ ires ]] <- x
         }
     }

result <- do.call(rbind, result)
dim(result)

Lazy because there might be a way of avoiding the loops. If this is just 
an example, is your real problem big?

Hope this helps,

Rui Barradas

Em 22-08-2012 14:23, bilelsan escreveu:
> Dear Ruser,
> Below, the deal (you can copy paste):
>
> r=3 ; set.seed(1)
> v <- matrix(c(rnorm(40)),10,4)
> for (j in 1:4){
>      for (i in 1:r){
>          x <- t(v[,j]^(i)*v[,1:4]^((r-(4-1)):r))
>          print(x)
>          }
>      }
>
> How to reach to " x " " row bind " inside or outside (preferred) of the
> loop.
> To obtain at the end something like:
>> x
> matrix( , 12*4, 10)
>
> Any help or advice is very welcome
> Many regards
> Bilel
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/rbind-in-a-loop-with-elements-tp4640976.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.




More information about the R-help mailing list