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

[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

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

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 -

Re: [R] Nested foreach loops in R repeating items

2014-02-05 Thread arun
Hi, Try ?duplicated()  apply(x,2,function(x) {x[duplicated(x)]-;x}) A.K. Hi all, I have a dataset of around a thousand column and a few thousands of rows. I'm trying to get all the possible combinations (without repetition) of the data columns and process them in parallel. Here's a

Re: [R] Nested foreach loops in R repeating items

2014-02-05 Thread Bert Gunter
I don't think you answered the OP's query, although I confess that I am not so sure I understand it either (see below). In any case, I believe the R level loop (i.e. apply()) is unnecessary. There is a unique (and a duplicated()) method for data frames, so simply unique(x) returns a data frame

[R] nested 'while' loops

2013-03-25 Thread Sahana Srinivasan
Hi everyone, I'm using the following code to go over every element of a data frame (row wise). The problem I am facing is that the outer 'x' variable is not incrementing itself, thus, only one row of values is obtained, and the program does not proceed to the next row. This is the code:

Re: [R] nested 'while' loops

2013-03-25 Thread Sahana Srinivasan
Hi, sorry that got sent without the output : Please ignore the aligning in the input, I am re-adding that here as well : 1GENEACDEFGHIKLMNPQRSTVWY2amt:Amet_00012902334171612422939635201325342732312 3amt:Amet_000219315421218835254372613914212030084 Output:

Re: [R] nested 'while' loops

2013-03-25 Thread Berend Hasselman
On 25-03-2013, at 18:43, Sahana Srinivasan sahanasrinivasan...@gmail.com wrote: Hi everyone, I'm using the following code to go over every element of a data frame (row wise). The problem I am facing is that the outer 'x' variable is not incrementing itself, thus, only one row of values is

Re: [R] nested 'while' loops

2013-03-25 Thread Sahana Srinivasan
while(x=21) { while(y=rown) { n-as.numeric(df[[y]][x]); if(n0) { while(k=lim) { k-k+1; } # while loop for k closes opdf[[y]][x]-sum; } # if statement closes y-y+1; } #while for y closes x-x+1; } #while with x

Re: [R] nested 'while' loops

2013-03-25 Thread Patrick Burns
Your immediate problem is that 'y' is not reset to 1. Easier code to write would be to use 'for' loops rather than 'while' loops. Better still would be to use neither if possible. I suspect that you are in Circle 3 of 'The R Inferno'. http://www.burns-stat.com/documents/books/the-r-inferno/

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 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-05 Thread R. Michael Weylandt michael.weyla...@gmail.com
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

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 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 to R

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

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 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 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 nick_pa...@yahoo.gr wrote: I found the way out - it was

[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 maximize

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,

[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

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

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

Re: [R] Nested For loops

2009-12-22 Thread ONKELINX, Thierry
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. As the same story

Re: [R] Nested For loops

2009-12-22 Thread baloo mia
] Nested For loops 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

Re: [R] Nested For loops

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

[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

Re: [R] Nested for loops

2009-07-14 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 time locating a quite subtle (at least

Re: [R] Nested for loops

2009-07-14 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 micknud...@gmail.com wrote: From: Michael Knudsen micknud...@gmail.com Subject: [R] Nested for loops To: r-help@r-project.org Received

Re: [R] Nested for loops

2009-07-14 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 8:03 AM, Moshe Olshanskym_olshan...@yahoo.com 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

Re: [R] Nested for loops

2009-07-14 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 8:20 AM, Michael Knudsenmicknud...@gmail.com 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

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 Knudsenmicknud...@gmail.com 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 =

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 Knudsenmicknud...@gmail.com 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

Re: [R] Nested for loops

2009-07-14 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 1:56 PM, David Winsemiusdwinsem...@comcast.net 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

Re: [R] Nested for loops

2009-07-14 Thread Michael Knudsen
On Tue, Jul 14, 2009 at 2:29 PM, Gabor Grothendieckggrothendi...@gmail.com 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] 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,