Re: [R] Avoiding for loops
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-1) { d[i] <- n - sum(x[1:i]) - i } for all of your loops, you will see already see very significant speedup. (If you look at my code formatting, and compare it with your own you may also benefit.) The bottom line: the point is not to avoid for-loops, but to speed up your code. Nb. if you want to avoid loops for some aesthetic reason, read about apply() and its siblings, and experiment with it. Of course, internally an apply() statement uses loops... NNb: Do you know how to profile your code? How do you know which part of your code is actually slowing it down? B. On Oct 25, 2015, at 6:42 AM, Maram SAlemwrote: > 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(x[(1):(i)])) - i > } > e<- n*(prod(d)) > > > 2nd loop > > LD<-list() > for (i in 1:(m-1)) > { > LD[[i]]<-seq(0,x[i],1) > } > > LD[[m]]<-seq(0,(n-m-sum(x)),1) > LED<-expand.grid (LD) > LED<-as.matrix(LED) > > > 3rd loop > > for (i in 1:(m-1)) > > { > > h[i]<- choose(x[i],LED[j,i]) > >} > > > > 4th loop > > > for (i in 1:(m-1)) > > { > > lm[i]<-(sum(LED[j,1:i])) + i > > } > > > I appreciate if anyone has any suggestions or references. > > > Thanks in advance. > > > Maram Salem > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Avoiding for loops
> On 25 Oct 2015, at 11:42, Maram SAlemwrote: > > 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(x[(1):(i)])) - i > } > e<- n*(prod(d)) > > On the basis of the other loops I presume you mean for( in in 1:(m-1)) in stead of what you wrote. You get the same result with d <- n - cumsum(x) - (1:(m-1)) Berend > 2nd loop > > LD<-list() > for (i in 1:(m-1)) > { > LD[[i]]<-seq(0,x[i],1) > } > > LD[[m]]<-seq(0,(n-m-sum(x)),1) > LED<-expand.grid (LD) > LED<-as.matrix(LED) > > > 3rd loop > > for (i in 1:(m-1)) > > { > >h[i]<- choose(x[i],LED[j,i]) > > } > > > > 4th loop > > > for (i in 1:(m-1)) > > { > >lm[i]<-(sum(LED[j,1:i])) + i > > } > > > I appreciate if anyone has any suggestions or references. > > > Thanks in advance. > > > Maram Salem > > [[alternative HTML version deleted]] > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Avoiding for loops
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(x[(1):(i)])) - i } e<- n*(prod(d)) 2nd loop LD<-list() for (i in 1:(m-1)) { LD[[i]]<-seq(0,x[i],1) } LD[[m]]<-seq(0,(n-m-sum(x)),1) LED<-expand.grid (LD) LED<-as.matrix(LED) 3rd loop for (i in 1:(m-1)) { h[i]<- choose(x[i],LED[j,i]) } 4th loop for (i in 1:(m-1)) { lm[i]<-(sum(LED[j,1:i])) + i } I appreciate if anyone has any suggestions or references. Thanks in advance. Maram Salem [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Avoiding for loops
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 Hasselmanwrote: > > > 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(x[(1):(i)])) - i > > } > > e<- n*(prod(d)) > > > > > On the basis of the other loops I presume you mean > > for( in in 1:(m-1)) > > in stead of what you wrote. > > You get the same result with > > d <- n - cumsum(x) - (1:(m-1)) > > > Berend > > > > 2nd loop > > > > LD<-list() > > for (i in 1:(m-1)) > > { > > LD[[i]]<-seq(0,x[i],1) > > } > > > > LD[[m]]<-seq(0,(n-m-sum(x)),1) > > LED<-expand.grid (LD) > > LED<-as.matrix(LED) > > > > > > 3rd loop > > > > for (i in 1:(m-1)) > > > > { > > > >h[i]<- choose(x[i],LED[j,i]) > > > > } > > > > > > > > 4th loop > > > > > > for (i in 1:(m-1)) > > > > { > > > >lm[i]<-(sum(LED[j,1:i])) + i > > > > } > > > > > > I appreciate if anyone has any suggestions or references. > > > > > > Thanks in advance. > > > > > > Maram Salem > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Avoiding for loops
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 <- numeric(m-1) for (i in 1:(m-1)) { d[i] <- n - sum(x[1:i]) - i } B. On Oct 25, 2015, at 8:13 AM, Boris Steipewrote: > 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-1) { > d[i] <- n - sum(x[1:i]) - i > } > > for all of your loops, you will see already see very significant speedup. (If > you look at my code formatting, and compare it with your own you may also > benefit.) The bottom line: the point is not to avoid for-loops, but to speed > up your code. > > Nb. if you want to avoid loops for some aesthetic reason, read about apply() > and its siblings, and experiment with it. Of course, internally an apply() > statement uses loops... > > NNb: Do you know how to profile your code? How do you know which part of your > code is actually slowing it down? > > > > B. > > > > > On Oct 25, 2015, at 6:42 AM, 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(x[(1):(i)])) - i >> } >> e<- n*(prod(d)) >> >> >> 2nd loop >> >> LD<-list() >> for (i in 1:(m-1)) >> { >> LD[[i]]<-seq(0,x[i],1) >> } >> >> LD[[m]]<-seq(0,(n-m-sum(x)),1) >> LED<-expand.grid (LD) >> LED<-as.matrix(LED) >> >> >> 3rd loop >> >> for (i in 1:(m-1)) >> >> { >> >> h[i]<- choose(x[i],LED[j,i]) >> >>} >> >> >> >> 4th loop >> >> >> for (i in 1:(m-1)) >> >> { >> >> lm[i]<-(sum(LED[j,1:i])) + i >> >> } >> >> >> I appreciate if anyone has any suggestions or references. >> >> >> Thanks in advance. >> >> >> Maram Salem >> >> [[alternative HTML version deleted]] >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] avoiding for loops
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, Cserháti Mátyás cs_ma...@yahoo.com wrote: Hello everyone, My name is Matthew and I'm new to this list. greetings to everyone. Sorry if I'm asking an old question, but I have an m x n matrix where the rows are value profiles and the columns are conditions. What I want to do is calculate the correlation between all possible pairs of rows. That is, if there are 10 rows in my matrix, then I want to calculate 10 x 10 = 100 correlation values (all against all). Now R is slow when I use two for loops. What kind of other function or tool can I use to get the job done more speedily? I've heard of tapply, lapply, etc. and by and aggregate. Any kind of help is gladly appreciated. Thanks, Matthew [Deleted the unnecessary digest] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] avoiding for loops
I have data that looks like this: df1 group id 1 red A 2 red B 3 red C 4 blue D 5 blue E 6 blue F I want a list of the groups containing vectors with the ids.I am avoiding subset(), as it is only recommended for interactive use. Here's what I have so far: df1 - data.frame(group=c(red, red, red, blue, blue, blue), id=c(A, B, C, D, E, F)) groups - levels(df1$group) byid - lapply(groups, ==, df1$group) groupIDX - lapply(byid, which) groupIDX [[1]] [1] 4 5 6 [[2]] [1] 1 2 3 This gives me a list of the indices for each group. I want to subset df1 based on this list. If I want just one group I can do this: df1[groupIDX[[1]],]$id [1] D E F What sort of statement should I use if I want a result like: [[1]] [1] D E F Levels: A B C D E F [[2]] [1] A B C Levels: A B C D E F So far, I've used a for loop. Can I express this with apply statements? groupIDs - list(1:length(groupIDX)) groupData- for (i in 1:length(groupIDX)) { groupIDs[[i]] - df1[groupIDX[[i]],]$id } __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] avoiding for loops
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 F with(df1, split(id, group)) $blue [1] D E F Levels: A B C D E F $red [1] A B C Levels: A B C D E F On Sun, Mar 25, 2012 at 4:46 PM, Ed Siefker ebs15...@gmail.com wrote: I have data that looks like this: df1 group id 1 red A 2 red B 3 red C 4 blue D 5 blue E 6 blue F I want a list of the groups containing vectors with the ids.I am avoiding subset(), as it is only recommended for interactive use. Here's what I have so far: df1 - data.frame(group=c(red, red, red, blue, blue, blue), id=c(A, B, C, D, E, F)) groups - levels(df1$group) byid - lapply(groups, ==, df1$group) groupIDX - lapply(byid, which) groupIDX [[1]] [1] 4 5 6 [[2]] [1] 1 2 3 This gives me a list of the indices for each group. I want to subset df1 based on this list. If I want just one group I can do this: df1[groupIDX[[1]],]$id [1] D E F What sort of statement should I use if I want a result like: [[1]] [1] D E F Levels: A B C D E F [[2]] [1] A B C Levels: A B C D E F So far, I've used a for loop. Can I express this with apply statements? groupIDs - list(1:length(groupIDX)) groupData- for (i in 1:length(groupIDX)) { groupIDs[[i]] - df1[groupIDX[[i]],]$id } __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Avoiding two loops
Hello, I have a R code for doing convolution of two functions: convolveSlow - function(x, y) { nx - length(x); ny - length(y) xy - numeric(nx + ny - 1) for(i in seq(length = nx)) { xi - x[[i]] for(j in seq(length = ny)) { ij - i+j-1 xy[[ij]] - xy[[ij]] + xi * y[[j]] } } xy } How do I reduce the 2 loops so that I can run the code faster? Thank you San [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Avoiding two loops
On 4 February 2011 at 14:03, sudhir cr wrote: | Hello, | | I have a R code for doing convolution of two functions: | | convolveSlow - function(x, y) { | nx - length(x); ny - length(y) | xy - numeric(nx + ny - 1) | for(i in seq(length = nx)) { | xi - x[[i]] | for(j in seq(length = ny)) { | ij - i+j-1 | xy[[ij]] - xy[[ij]] + xi * y[[j]] | } | } | xy | } | | How do I reduce the 2 loops so that I can run the code faster? Maybe by reading the answer to the _exact same question_ you appear to have asked on SO yesterday? http://stackoverflow.com/questions/4894506/avoid-two-for-loops-in-r Dirk | | Thank you | San | | [[alternative HTML version deleted]] | | __ | R-help@r-project.org mailing list | https://stat.ethz.ch/mailman/listinfo/r-help | PLEASE do read the posting guide http://www.R-project.org/posting-guide.html | and provide commented, minimal, self-contained, reproducible code. -- Dirk Eddelbuettel | e...@debian.org | http://dirk.eddelbuettel.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Avoiding two loops
On Fri, Feb 04, 2011 at 02:03:22PM -0500, sudhir cr wrote: Hello, I have a R code for doing convolution of two functions: convolveSlow - function(x, y) { nx - length(x); ny - length(y) xy - numeric(nx + ny - 1) for(i in seq(length = nx)) { xi - x[[i]] for(j in seq(length = ny)) { ij - i+j-1 xy[[ij]] - xy[[ij]] + xi * y[[j]] } } xy } How do I reduce the 2 loops so that I can run the code faster? Hello: Convolution of two vectors may be computed also using matrix reshaping without a loop. For example convolution - function(x, y) { # more efficient if length(x) = length(y) m - length(x) n - length(y) zero - matrix(0, nrow=n, ncol=n) a - rbind(x %o% y, zero) k - m + n - 1 b - matrix(c(a)[1:(n*k)], nrow=k, ncol=n) rowSums(b) } Testing this on computing the product of the polynomials (1+t)^4 (1+t)^3 yields x - choose(4, 0:4) y - choose(3, 0:3) convolution(x, y) [1] 1 7 21 35 35 21 7 1 which is the same as choose(7, 0:7). See also ?convolve. Hope this helps. Petr Savicky. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Avoiding for loops
Hi, I'm trying to normalize some data. My data is organized by groups. I want to normalize PER GROUP as opposed to over the entire data set. The current double loop that I'm using takes almost an hour to run on about 30,000 rows of data in 2,500 groups. I'm currently doing this: - for(group in unique(data$group)){ sum_V1 - sum(data$V1[data$group == group]) for(subject in data$subject[data$group == group]){ data$V1_norm[(data$group == group data$subject == subject)] - data$V1[(data$group == group data$subject == subject)] / sum_V1 } } - Can anyone point me to a faster way to do this in R. Thanks! -N __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Avoiding for loops
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 data is organized by groups. I want to normalize PER GROUP as opposed to over the entire data set. The current double loop that I'm using takes almost an hour to run on about 30,000 rows of data in 2,500 groups. I'm currently doing this: - for(group in unique(data$group)){ sum_V1 - sum(data$V1[data$group == group]) for(subject in data$subject[data$group == group]){ data$V1_norm[(data$group == group data$subject == subject)] - data$V1[(data$group == group data$subject == subject)] / sum_V1 } } - Can anyone point me to a faster way to do this in R. Thanks! -N __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Dimitris Rizopoulos Assistant Professor Department of Biostatistics Erasmus University Medical Center Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands Tel: +31/(0)10/7043478 Fax: +31/(0)10/7043014 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Avoiding for loops
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(x) x/sum(x))) I hope it helps. Best, Dimitris Noah Silverman wrote: Hi, I'm trying to normalize some data. My data is organized by groups. I want to normalize PER GROUP as opposed to over the entire data set. The current double loop that I'm using takes almost an hour to run on about 30,000 rows of data in 2,500 groups. I'm currently doing this: - for(group in unique(data$group)){ sum_V1 - sum(data$V1[data$group == group]) for(subject in data$subject[data$group == group]){ data$V1_norm[(data$group == group data$subject == subject)] - data$V1[(data$group == group data$subject == subject)] / sum_V1 } } - Can anyone point me to a faster way to do this in R. Thanks! -N __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Avoiding for loops
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))) That didn't work. I got a lot of NaN -N On 11/2/09 3:30 AM, Peter Dalgaard wrote: 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(x) x/sum(x))) I hope it helps. Best, Dimitris Noah Silverman wrote: Hi, I'm trying to normalize some data. My data is organized by groups. I want to normalize PER GROUP as opposed to over the entire data set. The current double loop that I'm using takes almost an hour to run on about 30,000 rows of data in 2,500 groups. I'm currently doing this: - for(group in unique(data$group)){ sum_V1- sum(data$V1[data$group == group]) for(subject in data$subject[data$group == group]){ data$V1_norm[(data$group == group data$subject == subject)] - data$V1[(data$group == group data$subject == subject)] / sum_V1 } } - Can anyone point me to a faster way to do this in R. Thanks! -N __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Avoiding FOR loops
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: x [,1] [,2] [,3] [1,]4 111 [2,]192 [3,]733 [4,]364 [5,]685 xk Var1 Var2 Var3 1 -0.25 1.75 0.5 20.75 1.75 0.5 31.75 1.75 0.5 42.75 1.75 0.5 53.75 1.75 0.5 64.75 1.75 0.5 75.75 1.75 0.5 86.75 1.75 0.5 97.75 1.75 0.5 10 -0.25 2.75 0.5 Here, X is a matrix of 3 variables in which each is of size 5 and XK are some values that correspond to each variable. For each variable, I want to do: |Xi - xkj| where i = 1 to 3 and j = 1 to 10 It looks as if a double FOR loop would work, but can the apply function work? Or some other function that is shorter than a FOR loop? Thank you, I hope this makes sense. Derek -- View this message in context: http://www.nabble.com/Avoiding-FOR-loops-tp14656517p14656517.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Avoiding FOR loops
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 correspond to each variable. For each variable, I want to do: |Xi - xkj| where i = 1 to 3 and j = 1 to 10 That should be i=1 to 5 I take it? If I understand what you want to do, then the outer function is the key: lapply(1:3, function(i) { outer(x[,i], xk[,i], -) } ) This should land you with a list of three 5x10 tables It looks as if a double FOR loop would work, but can the apply function work? Or some other function that is shorter than a FOR loop? Thank you, I hope this makes sense. Derek Haris Skiadas Department of Mathematics and Computer Science Hanover College __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.