[R] Help on vectorizing

2008-04-17 Thread Vidyut (Yahoo)
Hi, I am a newbie at R and was thinking of ways to vectorize this loop but can't quite figure out if anything more can be done : Here the dimensions of the arrays are (N=2000, K=2, D=1000): responsibilities : N * K pi: K corp: N*D theta: K*D for (n in 1:N) { for (k in 1:K

Re: [R] Help on vectorizing

2008-03-09 Thread Charles C. Berry
On Sun, 9 Mar 2008, Gabor Grothendieck wrote: > Try this: > > r <- unlist(sapply(x, function(x) c(rep(0, x), 1))) And in large problems ( length(x) >> 1000 ), this will be faster still r <- rep( rep( 0:1, length(x) ), rbind(x,1) ) HTH, Chuck > x2 <- diff(c(0, which(r==1))) - 1 > iden

Re: [R] Help on vectorizing

2008-03-09 Thread Gabor Grothendieck
Try this: r <- unlist(sapply(x, function(x) c(rep(0, x), 1))) x2 <- diff(c(0, which(r==1))) - 1 identical(x, x2) On Sun, Mar 9, 2008 at 7:55 PM, remko duursma <[EMAIL PROTECTED]> wrote: > > Dear R-helpers, > > I have two problems that I don't know how to vectorize (but would like to > because m

[R] Help on vectorizing

2008-03-09 Thread remko duursma
Dear R-helpers, I have two problems that I don't know how to vectorize (but would like to because my current solution is slow). # 1. #I have a vector x: x <- c(3, 0, 1, 0, 2, 2, 2, 0, 4, 2) #I want this translated into a new vector based on x,so that each element of x #is the number of z