Re: [R] nested for loops too slow

2015-04-12 Thread Bert Gunter
Well, sort of... aggregate() is basically a wrapper for lapply(), which ultimately must loop over the function call at the R interpreter level, as opposed to vectorized functions that loop at the C level and hence can be orders of magnitude faster. As a result, there is often little difference in

Re: [R] nested for loops too slow

2015-04-12 Thread Thierry Onkelinx
You don't need loops at all. grw <- aggregate(gw ~ ts + ISEG + iter, data = dat, FUN = sum) GRW <- aggregate(gw ~ ts + ISEG, data = grw, FUN = function(x){max(x) - min(x)}) DC <- aggregate(div ~ ts + ISEG, data = subset(dat, IRCH == 1), FUN = function(x){max(x) - min(x)}) iter <- a

Re: [R] nested for loops too slow

2015-04-12 Thread J Robertson-Burns
You are certainly in Circle 2 of 'The R Inferno', which I suspect is where almost all of the computation time is coming from. Instead of doing: divChng <- rbind(divChng,c(datTS$ts[1], SEG[j], DC, GRW, max(datTS$iter))) it would be much better to create 'divChng' to be the final length and then

[R] nested for loops too slow

2015-04-12 Thread Morway, Eric
The small example below works lighting-fast; however, when I run the same script on my real problem, a 1Gb text file, the for loops have been running for over 24 hrs and I have no idea if the processing is 10% done or 90% done. I have not been able to figure out a betteR way to code up the materia

Re: [R] nested "for" loops

2011-11-06 Thread nick_pan
I sent a post yesterday that I found out why my function didn't work. It's ok now it works. Thank you all. -- View this message in context: http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3994996.html Sent from the R help mailing list archive at Nabble.com. __

Re: [R] nested "for" loops

2011-11-05 Thread R. Michael Weylandt
No idea how this relates to what you said originally but glad you got it all worked out. And let us all reiterate: really, don't use nested for loops...there's a better way: promise! Michael On Sat, Nov 5, 2011 at 2:20 PM, nick_pan wrote: > I found the way out - it was because the borders of th

Re: [R] nested "for" loops

2011-11-05 Thread Jeff Newmiller
Bert, this is not helpful. Since for loops and apply functions are not vectorized, why are you admonishing Carl that vectorizing doesn't always speed up algorithms? He didn't reference apply functions as being vectorized. But you seem to be doing so. I would assert that vectorizing DOES always

Re: [R] nested "for" loops

2011-11-05 Thread nick_pan
I found the way out - it was because the borders of the vectors was close enough thats why I had the same result while I was adding points to the sequence. The example I gave was irrelevant but I made in order to find out that the problem was. Thank you all for your answers. -- View this message

Re: [R] nested "for" loops

2011-11-05 Thread Bert Gunter
Carl: "Almost anything you can do in a for() loop can be done easier and faster with vectorization.-- " That is false: while this is certainly true for a great many basic vectorized operations, it is certainly false for most other things -- simulations are a typical example. Note that __ply type o

Re: [R] nested "for" loops

2011-11-05 Thread David Winsemius
You need to define "l" as a dimensioned object , either a vector or an array, and then assign the value of your calculation to the correctly indexed "location" in that object. Otherwise you are just overwriting the value each time through the loop. Use these help pages (and review "Introduction

Re: [R] nested "for" loops

2011-11-05 Thread Carl Witthoft
If in fact this is homework, you will do yourself, your classmates, and possibly your teacher if you let them know that, at least in R, almost anything you can do in a for() loop can be done easier and faster with vectorization. If you teacher can't comprehend this, get him fired. a<-c(4,6,3)

Re: [R] nested "for" loops

2011-11-05 Thread R. Michael Weylandt
Why do you need to do it with nested for loops? It is of course possible - and I hinted how to do it in my first email - but there's no reason as far as I can see to do so, particularly as a means of MLE. Sounds suspiciously like homework... Michael On Nov 4, 2011, at 10:14 PM, nick_pan wrote

Re: [R] nested "for" loops

2011-11-05 Thread nick_pan
Thank you , this works but I have to do it with nested for loops... Could you suggest me a way ? -- View this message in context: http://r.789695.n4.nabble.com/nested-for-loops-tp3992089p3992324.html Sent from the R help mailing list archive at Nabble.com. __

Re: [R] nested "for" loops

2011-11-04 Thread R. Michael Weylandt
Your problem is that you redefine l each time through the loops and don't record old values; you could do so by using c() for concatenation, but perhaps this is what you are looking for: exp(rowSums(log(expand.grid(V1, V2, V3 Hope this helps, Michael On Fri, Nov 4, 2011 at 7:49 PM, nick_pan

[R] nested "for" loops

2011-11-04 Thread nick_pan
Hi all , I have written a code with nested "for" loops . The aim is to estimate the maximum likelihood by creating 3 vectors with the same length( sequence ) and then to utilize 3 "for" loops to make combinations among the 3 vectors , which are (length)^3 in number , and find the one that maximiz

Re: [R] nested for loops

2010-07-05 Thread jim holtman
What do you want to do with the data being genereated? In the loop you have, it will just return the last value generated. Let me ask my favorite question: "What is the problem you are trying to solve". If you get a memory problem with expand.grid, then if you are trying to store the values in t

Re: [R] nested for loops

2010-07-05 Thread Romain Francois
Le 05/07/10 23:06, Senay ASMA a écrit : Dear Admin, I will appreciate if you advise me an effective way to write the following R code including nested for loops. I cannot do it by using expand.grid function because it results with memory allocation problems. Thanks for your time and considerati

[R] nested for loops

2010-07-05 Thread Senay ASMA
Dear Admin, I will appreciate if you advise me an effective way to write the following R code including nested for loops. I cannot do it by using expand.grid function because it results with memory allocation problems. Thanks for your time and consideration. for(d1 in 0:n){ for(d2 in 0:n){ for(d3

Re: [R] Nested For loops

2009-12-22 Thread Paul Hiemstra
k is the lag. Baloo --- On Tue, 12/22/09, ONKELINX, Thierry wrote: From: ONKELINX, Thierry Subject: RE: [R] Nested For loops To: "baloo mia" , r-help@r-project.org Date: Tuesday, December 22, 2009, 1:00 AM Baloo, Why don't you use the built-in acf

Re: [R] Nested For loops

2009-12-22 Thread baloo mia
In the ACF(nlme) the normalization of the numerator has been done by N and I want to normalize it by N-k, where N is the observations and k is the lag. Baloo --- On Tue, 12/22/09, ONKELINX, Thierry wrote: From: ONKELINX, Thierry Subject: RE: [R] Nested For loops To: "baloo mia"

Re: [R] Nested For loops

2009-12-22 Thread ONKELINX, Thierry
~ John Tukey -Oorspronkelijk bericht- Van: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] Namens baloo mia Verzonden: dinsdag 22 december 2009 2:07 Aan: r-help@r-project.org Onderwerp: [R] Nested For loops Dear R experts, Might be very simple question to ask but would be insightful

[R] Nested For loops

2009-12-21 Thread baloo mia
Dear R experts, Might be very simple question to ask but would be insightful. As the same story of nested "for loops". following is the code that I am using to get the autocorrelation function of the sample data. I have tried to get rid of for loops but since I am touching R after such a long t

Re: [R] Nested for loops

2009-07-14 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 2:29 PM, Gabor Grothendieck wrote: > seq. <- function(from, to) seq(from = from, length = max(0, to - from + 1)) Really nice! Thank you! -- Michael Knudsen micknud...@gmail.com http://lifeofknudsen.blogspot.com/ __ R-help@r-pr

Re: [R] Nested for loops

2009-07-14 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 1:56 PM, David Winsemius wrote: >> temp[ upper.tri(temp) ] >  [1]  7 13 14 19 20 21 25 26 27 28 31 32 33 34 35 Thanks! I didn't know about that function; it certainly makes things a lot easier. For example, until now I have used the following, homemade expression (1:N^2)[

Re: [R] Nested for loops

2009-07-14 Thread Gabor Grothendieck
Try this: seq. <- function(from, to) seq(from = from, length = max(0, to - from + 1)) seq.(11, 10) On Tue, Jul 14, 2009 at 1:38 AM, Michael Knudsen wrote: > Hi, > > I have spent some time locating a quite subtle (at least in my > opinion) bug in my code. I want two nested for loops traversing

Re: [R] Nested for loops

2009-07-14 Thread David Winsemius
On Jul 14, 2009, at 2:25 AM, Michael Knudsen wrote: On Tue, Jul 14, 2009 at 8:20 AM, Michael Knudsen wrote: What do you mean? It looks a like a very general solution to me. Just got an email suggesting using the functions col and row. For example temp = matrix(c(1:36),nrow=6) which(co

Re: [R] Nested for loops

2009-07-13 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 8:20 AM, Michael Knudsen wrote: > What do you mean? It looks a like a very general solution to me. Just got an email suggesting using the functions col and row. For example temp = matrix(c(1:36),nrow=6) which(col(temp)>row(temp)) This gives the indices (in the matrix vie

Re: [R] Nested for loops

2009-07-13 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 8:03 AM, Moshe Olshansky wrote: > Make it > for (i in 1:9) Thanks. That's also how I solved the problem myself. I just somehow think it makes my code look rather clumsy and opaque. Maybe I just have to get used to this kind of nasty tricks. > This is not the general solut

Re: [R] Nested for loops

2009-07-13 Thread Moshe Olshansky
Make it for (i in 1:9) This is not the general solution, but in your case when i=10 you do not want to do anything. --- On Tue, 14/7/09, Michael Knudsen wrote: > From: Michael Knudsen > Subject: [R] Nested for loops > To: r-help@r-project.org > Received: Tuesday, 14 July, 2009,

Re: [R] Nested for loops

2009-07-13 Thread Daniel Nordlund
> -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Michael Knudsen > Sent: Monday, July 13, 2009 10:39 PM > To: r-help@r-project.org > Subject: [R] Nested for loops > > Hi, > > I have spent some ti

[R] Nested for loops

2009-07-13 Thread Michael Knudsen
Hi, I have spent some time locating a quite subtle (at least in my opinion) bug in my code. I want two nested for loops traversing the above-diagonal part of a square matrix. In pseudo code it would something like for i = 1 to 10 { for j = i+1 to 10 { // do something } } However,