Re: [R] crazy loop error.

2011-01-26 Thread Petr Savicky
On Tue, Jan 25, 2011 at 08:58:31AM +, Prof Brian Ripley wrote: [...] > >If k may be 0, then it is better to use > > > > for (n in seq(length=k)) > > > >since seq(length=0) has length 0. > > Since you keep mentioning that, it is actually much better to use > seq_len(k) (and seq_along(x) instea

Re: [R] crazy loop error.

2011-01-25 Thread Ivan Calandra
Now I understand what the difference between a primitive and a non-primitive! Thanks for the clarification! Ivan Le 1/25/2011 18:03, Bert Gunter a écrit : Well, I'm not Prof. Ripley, but the answer is: Look at the code. seq_len, seq.int, and seq_along call Primitives, which are implemented in C

Re: [R] crazy loop error.

2011-01-25 Thread Bert Gunter
Well, I'm not Prof. Ripley, but the answer is: Look at the code. seq_len, seq.int, and seq_along call Primitives, which are implemented in C, and therefore MUCH faster than seq(), which is implemented as pure R code (and is also a generic, so requires method dispatch). Though for small n (up to a f

Re: [R] crazy loop error.

2011-01-25 Thread Petr PIKAL
Hi r-help-boun...@r-project.org napsal dne 25.01.2011 10:58:36: > ooh.. I have another question. > What if I want to add the value in the vector a to the hello each time it > prints. > Here is your output > > a <- c(2,3,5,5,5,6,6,7) > mapply(rep, "hello", rle(a)$lengths, USE.NAMES = FALSE) > >

Re: [R] crazy loop error.

2011-01-25 Thread Roy Mathew
ooh.. I have another question. What if I want to add the value in the vector a to the hello each time it prints. Here is your output a <- c(2,3,5,5,5,6,6,7) mapply(rep, "hello", rle(a)$lengths, USE.NAMES = FALSE) [[1]] [1] "hello" [[2]] [1] "hello" [[3]] [1] "hello" "hello" "hello" [[4]] [1] "

Re: [R] crazy loop error.

2011-01-25 Thread Ivan Calandra
Mr Ripley, May I ask why seq_len() and seq_along() are better than seq()? Thanks, Ivan Le 1/25/2011 09:58, Prof Brian Ripley a écrit : On Tue, 25 Jan 2011, Petr Savicky wrote: On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote: Thanks for the reply Erik, As you mentioned, grouping co

Re: [R] crazy loop error.

2011-01-25 Thread Roy Mathew
Dear Erik, Thanks for the mapply idea. I never got around to understand all those apply functions. I am still curious as to why the other loop didnt work. I even tried the debug but doesnt help. Anyway I will leave that for now. Thanks a lot for your help. Regards, Roy On Mon, Jan 24, 2011 at 11:

Re: [R] crazy loop error.

2011-01-25 Thread Petr Savicky
On Tue, Jan 25, 2011 at 09:05:03AM +0100, Petr Savicky wrote: [...] > to foreach loop in Perl. If v is a vector, then > > for (n in v) > > first creates the vector v and then always performs length(v) iterations. I forgot that ‘break’ may stop the loop. See ?"for" for further information. In p

Re: [R] crazy loop error.

2011-01-25 Thread Prof Brian Ripley
On Tue, 25 Jan 2011, Petr Savicky wrote: On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote: Thanks for the reply Erik, As you mentioned, grouping consecutive elements of 'a' was my idea. I am unaware of any R'ish way to do it. It would be nice if someone in the community knows this. T

Re: [R] crazy loop error.

2011-01-25 Thread Petr PIKAL
Hi r-help-boun...@r-project.org napsal dne 24.01.2011 23:18:35: > Thanks for the reply Erik, As you mentioned, grouping consecutive elements > of 'a' was my idea. > I am unaware of any R'ish way to do it. It would be nice if someone in the > community knows this. > > The error resulting in th

Re: [R] crazy loop error.

2011-01-25 Thread Petr Savicky
On Mon, Jan 24, 2011 at 11:18:35PM +0100, Roy Mathew wrote: > Thanks for the reply Erik, As you mentioned, grouping consecutive elements > of 'a' was my idea. > I am unaware of any R'ish way to do it. It would be nice if someone in the > community knows this. > > The error resulting in the NA was

Re: [R] crazy loop error.

2011-01-24 Thread Roy Mathew
Thanks for the reply Erik, As you mentioned, grouping consecutive elements of 'a' was my idea. I am unaware of any R'ish way to do it. It would be nice if someone in the community knows this. The error resulting in the NA was pretty easy to fix, and my loop works, but the results are still wrong (

Re: [R] crazy loop error.

2011-01-24 Thread Erik Iverson
Roy Mathew wrote: Thanks for the reply Erik, As you mentioned, grouping consecutive elements of 'a' was my idea. I am unaware of any R'ish way to do it. It would be nice if someone in the community knows this. Is this the idea you're trying to execute? It uses ?rle and ?mapply. a <- c(2,3

Re: [R] crazy loop error.

2011-01-24 Thread Petr Savicky
On Mon, Jan 24, 2011 at 07:16:58PM +0100, Roy Mathew wrote: > Dear R-users, > This is a loop which is part of a bigger script. I managed to isolate the > error in this loop and simplified it to the bare minimum and made it > self-contained. > > a<-c(2,3,4,5,5,5,6,6,6,7) > > for(n in 1:10) > { > p

Re: [R] crazy loop error.

2011-01-24 Thread Erik Iverson
Roy, I have no idea what you're actually trying to do here, but it looks like there would be a more natural R'ish way if you're concerned about grouping consecutive elements of 'a'. At any rate, within your while loop, you're incrementing n by 1, and eventually n will be 10, which will be transf

[R] crazy loop error.

2011-01-24 Thread Roy Mathew
Dear R-users, This is a loop which is part of a bigger script. I managed to isolate the error in this loop and simplified it to the bare minimum and made it self-contained. a<-c(2,3,4,5,5,5,6,6,6,7) for(n in 1:10) { print(paste("n: ",n)) z1<-a[n] #make a list container ldata<-list() t=1 while(z1