Re: [R] Avoiding for loops

2015-10-25 Thread Maram SAlem
Thanks a lot Boris and Berend. I'll consider the brackets ((m-1) in every loop). In addition, I'll read more on profiling my code. In fact,I'm using the apply () in another part of my code. Thanks again for helping. Maram Salem On 25 October 2015 at 14:26, Berend Hasselman wrote: > > > On 25

Re: [R] Avoiding for loops

2015-10-25 Thread Berend Hasselman
> On 25 Oct 2015, at 11:42, Maram SAlem wrote: > > Hi All, > > I wonder if I can avoid the for() loop in any of the following loops.These > loops are a part of a larger code which I'm trying to accelerate. > > n=6 > m=4 > x<-c(0,1,1) > > 1st loop > > for (i in 1:m-1) > { > d[i]<- n- (sum

Re: [R] Avoiding for loops

2015-10-25 Thread Boris Steipe
Sorry - I just noticed you actually have an error in your code: you had parentheses everywhere they were not needed and I overlooked you had not put them where they actually are needed. It has to be for (i in 1:(m-1)) ..., not as you wrote. I'm sure you'll understand the difference. d <- numeri

Re: [R] Avoiding for loops

2015-10-25 Thread Boris Steipe
If this code is slow it is not because you are using loops, but because you are dynamically building your vectors and lists and their size needs to change with each iteration causing significant unnecessary computational overhead. If you simply do something like d <- numeric(m-1) for (i in 1:m-

Re: [R] avoiding for loops

2012-04-06 Thread R. Michael Weylandt
Usually you can just use cor() and it will do all the possibilities directly: x <- matrix(rnorm(100), ncol = 10) cor(x) But that works on the columns, so you'll need to transpose things if you want all possible row combinations: cor(t(x)) Hope this helps, Michael On Fri, Apr 6, 2012 at 9:57 AM,

Re: [R] avoiding for loops

2012-03-25 Thread Richard M. Heiberger
> df1 <- data.frame(group=c("red", "red", "red", "blue", "blue", "blue"), id=c("A", "B", "C", "D", "E", "F")) df1 <- data.frame(group=c("red", "red", "red", "blue", "blue", + "blue"), id=c("A", "B", "C", "D", "E", "F")) > df1 group id 1 red A 2 red B 3 red C 4 blue D 5 blue E 6 blue

Re: [R] Avoiding for loops

2009-11-02 Thread Noah Silverman
Wow, That's nice. Should work well, but I just realized that I missed something in explaining my code. I need to calculate the exp function on X so it should be exp(x) / sum(exp(x)) for each group I tried this with: foo <- ave(rawdata$foo,rawdata$code,FUN=function(x) exp(x) / sum(exp(x)))

Re: [R] Avoiding for loops

2009-11-02 Thread Peter Dalgaard
Dimitris Rizopoulos wrote: > you could try something along these lines: > > data <- data.frame(y = rnorm(100), group = rep(1:10, each = 10)) > > data$sum <- ave(data$y, data$group, FUN = sum) > data$norm.y <- data$y / data$sum > data .. or even transform(data, norm=ave(y, group, FUN = function(

Re: [R] Avoiding for loops

2009-11-02 Thread Dimitris Rizopoulos
you could try something along these lines: data <- data.frame(y = rnorm(100), group = rep(1:10, each = 10)) data$sum <- ave(data$y, data$group, FUN = sum) data$norm.y <- data$y / data$sum data I hope it helps. Best, Dimitris Noah Silverman wrote: Hi, I'm trying to normalize some data. My

Re: [R] Avoiding FOR loops

2008-01-06 Thread Charilaos Skiadas
On Jan 6, 2008, at 7:55 PM, dxc13 wrote: > > useR's, > > I would like to know if there is a way to avoid using FOR loops to > perform > the below calculation. > > Consider the following data: > snip > Here, X is a matrix of 3 variables in which each is of size 5 and > XK are > some values that