Re: [R] Avoiding a loop

2011-04-08 Thread Niels Richard Hansen
I believe the solutions proposed ignore the recursive nature of the original problem and hence produce wrong solutions. P <- c(5, 7, 6.01, 6.01, 7) m <- rep(6, 5) S0 <- as.numeric(P>(m*1.005)) Then the original loop from Worik gives S <- S0 for(i in 2:length(S)){ if(S[i]==0 && S[i-1] == 1){

Re: [R] Avoiding a loop

2011-04-08 Thread jim holtman
Use 'diff' to determine where the changes are: > S <- sample(0:1,30,TRUE) > S [1] 0 0 1 1 0 1 1 1 1 0 0 0 1 0 1 0 1 1 0 1 1 0 1 0 0 0 0 0 1 0 > which(diff(S) == -1) [1] 4 9 13 15 18 21 23 29 > then use the indices for the other processing. On Thu, Apr 7, 2011 at 10:30 PM, Worik R wrote: > Fr

Re: [R] Avoiding a loop

2011-04-08 Thread Daniel Malter
Hi, this response uses the previous responses with an example: #Assume you have 100 observations n=100 #Simulate a time series of prices error=rnorm(n,0,3) raw.price=rpois(n,100) lag.price=c(rpois(1,100),raw.price[1:99]) price=lag.price+error #Say you want the moving average based on this #and

Re: [R] Avoiding a loop

2011-04-08 Thread Juan Carlos Borrás
Kenn, I find your solution more elegant. 2011/4/8 Kenn Konstabel : > 2011/4/8 Juan Carlos Borrás : >> #Use the indexes of S in a sapply function. >> >> N <- 10 >> S <- sample(c(0,1), size=N, replace=TRUE) >> v1 <- sapply(c(1:N-1), function(i) S[i]&&S[i+1]) > > You can achieve the same v1 using > >

Re: [R] Avoiding a loop

2011-04-08 Thread Kenn Konstabel
2011/4/8 Juan Carlos Borrás : > #Use the indexes of S in a sapply function. > > N <- 10 > S <- sample(c(0,1), size=N, replace=TRUE) > v1 <- sapply(c(1:N-1), function(i) S[i]&&S[i+1]) You can achieve the same v1 using v1.2 <- S[2:N-1] & S[2:N] .. or if you insist on having NA as the first elemen

Re: [R] Avoiding a loop

2011-04-07 Thread Juan Carlos Borrás
#Use the indexes of S in a sapply function. N <- 10 S <- sample(c(0,1), size=N, replace=TRUE) v1 <- sapply(c(1:N-1), function(i) S[i]&&S[i+1]) # Then v2 <- (P > m) # And I guess you can fill up the rest. Beware of the boundary condition (the NA in v1) Cheers, jcb! ___ http:/

[R] Avoiding a loop

2011-04-07 Thread Worik R
Friends. I cannot simplify this much, and I think the loop is unavoidable. As a recovering C programmer I want to avoid loops and in cases like this I almost allways can by using an apply function. But I suspect in this case there is nothing I can do. It is a finance example where a price serie

Re: [R] Avoiding a loop

2008-04-21 Thread Jorge Ivan Velez
Thank you so much to Jim and Mark for their advices. Now I solved the problem I had using a new approach. Best, Jorge On Mon, Apr 21, 2008 at 12:53 PM, jim holtman <[EMAIL PROTECTED]> wrote: > Will this do it for you: > > # Seed and data frames X, Y and Z > set.seed(123) > X=matrix(rnorm(300),

Re: [R] Avoiding a loop

2008-04-21 Thread jim holtman
Will this do it for you: # Seed and data frames X, Y and Z set.seed(123) X=matrix(rnorm(300),ncol=5) Y=matrix(rpois(300,10),ncol=5) Z=matrix(rexp(300,1),ncol=5) index <- seq(1, by=3, length=nrow(X)) FINAL <- matrix(ncol=5, nrow=3*nrow(X)) FINAL[index,] <- X FINAL[index + 1,] <- Y FINAL[index + 2,

[R] Avoiding a loop

2008-04-21 Thread Jorge Ivan Velez
Dear R-users, I've been working with three different data sets (X, Y and Z) with the same dimension (i.e, n \times k). What I needed to do was to conform a 4th data set, i.e. FINAL, which first row was the X's first row, its second row was the Y's first row, and its third row was the Z's first row

Re: [R] avoiding a loop?

2007-10-15 Thread tom sgouros
I knew it was simple. Thanks very much. -tom jim holtman <[EMAIL PROTECTED]> wrote: > ?table to count the factors > > > x > [1] "a" "b" "c" "d" "e" > > paste(head(x, -1), tail(x, -1), sep='') > [1] "ab" "bc" "cd" "de" > > > On 10/15/07, Tom Sgouros <[EMAIL PROTECTED]> wrote: > > > > Hi

Re: [R] avoiding a loop?

2007-10-15 Thread jim holtman
?table to count the factors > x [1] "a" "b" "c" "d" "e" > paste(head(x, -1), tail(x, -1), sep='') [1] "ab" "bc" "cd" "de" On 10/15/07, Tom Sgouros <[EMAIL PROTECTED]> wrote: > > Hi All: > > I feel like there must be a slick R-native no-loop way to get the counts > for the entries in a factor,

[R] avoiding a loop?

2007-10-15 Thread Tom Sgouros
Hi All: I feel like there must be a slick R-native no-loop way to get the counts for the entries in a factor, but I'm unable to see how. Right now I'm doing this: hn.ent<-data.frame(rep(0,length(levels(hnf))), row.names=levels(hnf)) colnames(hn.ent)<-c("count") for (lev in levels(hnf)) { hn.e