Re: [R] Avoiding loops using 'for' and pairwise comparison of columns

2013-06-24 Thread Blaser Nello
Here's a possible solution to avoid the loop k <- as.matrix(expand.grid(1:ncol(x),1:ncol(x))) a1 <- as.data.frame(matrix(sapply(1:nrow(k), function(n) agree(x[,k[n,]])$value), nrow=ncol(x))) colnames(a1) <- colnames(x) rownames(a1) <- colnames(x) > identical(a, a1) [1] TRUE Or if you want to avo

Re: [R] Avoiding loops to detect number of coincidences

2011-07-12 Thread Sarah Goslee
Hi Trying, It would be helpful if you provided reproducible examples. It would also be polite to sign a name so that we have something by which to address you. On Tue, Jul 12, 2011 at 8:00 AM, Trying To learn again wrote: > Hi all, > > I have this information on a file ht.txt, imagine it is a da

Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-30 Thread Uwe Ligges
On 30.05.2010 19:23, Alan Lue wrote: Is there a performance advantage to doing this, as opposed to growing the vector within the loop? I suppose R could have to dynamically reallocate memory at some point? Right, but that takes time since memory management is always expensive (and this way

Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-30 Thread Alan Lue
Is there a performance advantage to doing this, as opposed to growing the vector within the loop? I suppose R could have to dynamically reallocate memory at some point? Alan 2010/5/30 Uwe Ligges : > > > On 26.05.2010 08:52, Alan Lue wrote: >> >> Come to think of it, we can't save the output of

Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-30 Thread Uwe Ligges
On 26.05.2010 08:52, Alan Lue wrote: Come to think of it, we can't save the output of each invocation and concatenate it later, since we need the output as input for the next iteration. Yes, but you can do it a bit cleverer than before by initializing to the fill length as in: r.seq <- nu

Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-26 Thread Dennis Murphy
Hi: On Tue, May 25, 2010 at 11:43 PM, Alan Lue wrote: > Since `for' loops are slow in R, and since `apply' functions are > faster, I was wondering whether there were a way to use an apply > function—or to otherwise avoid using a loop—when iterating over a > statement that updates its input. > T

Re: [R] Avoiding Loops When Iterating Over Statement That Updates Its Input

2010-05-25 Thread Alan Lue
Come to think of it, we can't save the output of each invocation and concatenate it later, since we need the output as input for the next iteration. Alan On Tue, May 25, 2010 at 11:43 PM, Alan Lue wrote: > Since `for' loops are slow in R, and since `apply' functions are > faster, I was wonderin

Re: [R] avoiding loops in equation

2009-10-17 Thread Julius Tesoro
thank you very much --- On Sat, 10/17/09, Kenn Konstabel wrote: > From: Kenn Konstabel > Subject: Re: [R] avoiding loops in equation Thank God for R-help mailing list. Thanks.. > To: "Julius Tesoro" > Date: Saturday, October 17, 2009, 1:51 PM > a3 <- sapply(acc,

Re: [R] Avoiding loops

2009-09-02 Thread William Dunlap
bco.com > -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Martin Morgan > Sent: Wednesday, September 02, 2009 9:17 AM > To: Alexander Shenkin > Cc: r-help@r-project.org; spec...@stat.berkeley.edu; > cbe

Re: [R] Avoiding loops

2009-09-02 Thread Martin Morgan
Alexander Shenkin wrote: > Though, from my limited understanding, the 'apply' family of functions > are actually just loops. Please correct me if I'm wrong. So, while > more readable (which is important), they're not necessarily more > efficient than explicit 'for' loops. Hi Allie -- This uses a

Re: [R] Avoiding loops

2009-09-02 Thread Charles C. Berry
On Tue, 1 Sep 2009, dolar wrote: Would like some tips on how to avoid loops as I know they are slow in R If I understand your criterion (and calling your data.frame 'dat'): criterion <- as.matrix(dist(dat$a)) <= 5 & outer(dat$a,dat$a,">=") criterion %*% as.matrix(dat[, c("b","c")]) b

Re: [R] Avoiding loops

2009-09-02 Thread Phil Spector
Another advantage of the apply family of functions is that they determine the size and type of their output in an efficient way, which is sometimes tricky when you write the loop yourself. - Phil Spector Statistica

Re: [R] Avoiding loops

2009-09-02 Thread stephen sefick
If you can do it- try a for loop and another solution to prove this to yourself. A for loop can get a little unwieldy for a novice like me to understand the code, but doable. The simpler the better, but they are not terribly slow. I have run into a couple of situations where a vectorized solutio

Re: [R] Avoiding loops

2009-09-02 Thread hadley wickham
> Would like some tips on how to avoid loops as I know they are slow in R They are not slow. They are slower than vectorised equivalents, but not slower than apply and friends. Hadley -- http://had.co.nz/ __ R-help@r-project.org mailing list https:/

Re: [R] Avoiding loops

2009-09-02 Thread Alexander Shenkin
Though, from my limited understanding, the 'apply' family of functions are actually just loops. Please correct me if I'm wrong. So, while more readable (which is important), they're not necessarily more efficient than explicit 'for' loops. allie On 9/2/2009 3:13 AM, Phil Spector wrote: > Here's

Re: [R] Avoiding loops

2009-09-02 Thread Phil Spector
Here's one way (assuming your data frame is named dat): with(dat, data.frame(a,t(sapply(a,function(x){ apply(dat[a - x >= -5 & a - x <= 0,c('b','c')],2,sum)} - Phil Spector St

Re: [R] Avoiding loops & apply -function

2008-11-05 Thread Yohan Chalabi
"DM" == David Masson <[EMAIL PROTECTED]> on Wed, 05 Nov 2008 15:13:37 +0100 DM> I have a question concerning avoiding loops. DM> I know the function "apply" and I have used it several times, but I feel DM> blocked DM> with this situation : DM> DM> E <- array(X, dim =

Re: [R] avoiding loops

2008-03-27 Thread Bill.Venables
Ingmar Visser asks: > > Thanks for this. > I was afraid someone was going to say this ... > Does this mean the only way of getting this to run faster is by > moving to C code? Perhaps, but that's not all that difficult for this kind of operation, surely? Even portability across platforms shoul

Re: [R] avoiding loops

2008-03-27 Thread Ingmar Visser
Thanks for this. I was afraid someone was going to say this ... Does this mean the only way of getting this to run faster is by moving to C code? The cases I'm thinking of applying this in have dimensions of A that are much larger than the example, eg n by n by T where n has a max of 10 or so b

Re: [R] avoiding loops

2008-03-26 Thread Bill.Venables
If you have lots of memory there is an obvious strategy: d12 <- prod(dim(A)[1:2]) A <- A * array(rep(B, each = d12), dim = dim(A)) I don't really see much wrong with the obvious for() loop, though: for(b in 1:length(B)) A[,,b] <- A[,,b] * B[b] Bill Venables CSIRO Laboratories PO Box 120, Cleve